Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transfer of cookies between the client and the server. #1449

Closed
FMakareev opened this issue Oct 30, 2017 · 1 comment
Closed

Transfer of cookies between the client and the server. #1449

FMakareev opened this issue Oct 30, 2017 · 1 comment

Comments

@FMakareev
Copy link

Hello. I use node.js as a layer between php server and client (browser). All requests go through graphql. Everything works well, but there is one problem - i cannot share cookie across graphql from client.
Here is the algorithm of actions:
The user makes a request to the graph and passes the scheme. The graph in the resolve method makes a request to api and passes the cookies. query method fetch api.
const AuthMutation = { type: ResponseType, args: { locale: { type: StringType }, password: { type: StringType }, username: { type: StringType } }, async resolve(parent, args, context) { const body = querystring.stringify(args); const headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Cookie': cookieToString(context.req.cookies) }; const {object} = await queryFetch(api.gqlAPI.auth, 'POST', headers, body); return object; }, };
how to pass cookies from the 'resolve' method to the client

@FMakareev
Copy link
Author

I found a solution.

in server.js:

app.use('/graphql', (req, res) => {
    return graphqlHTTP({
        schema,
        graphiql: __DEV__,
        context: {req, res},
    })(req, res)
})

example graphql mutation:

const AuthMutation = {
    type: ResponseType,
    args: {
        locale: {
            type: StringType
        },
        password: {
            type: StringType
        },
        username: {
            type: StringType
        }
    },
    async resolve(parent, args, context) {
        const body = querystring.stringify(args);
        const headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Cookie': cookieToString(context.req.cookies)
        };
        const {object} = await new Promise((resolve, reject) => {
            headers["Connection"] = "Keep-Alive";
            fetch(api.gqlAPI.auth, {
                    method: 'POST',
                    credentials: 'same-origin',
                    cache: 'no-cache',
                    headers: headers,
                    ...(body ? {body: body} : null),
                }
            ).then((response) => {
               // Recording the cookies received from the php server in response to the graphql
                context.res.set('set-cookie', response.headers.get('set-cookie'))
                if (response.status >= 200 && response.status < 300) {
                    return Promise.resolve(response);
                } else {
                    return Promise.reject(response.status);
                }
            }).then((response) => {
                return response.json()
            }).then((response) => {
                return resolve(response);
            }).catch((error) => {
                reject(error);
            });
        });
        return object;
    },
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant