vibhanshu909 / graphql-server

MIT License
7 stars 0 forks source link

Usage of "apollo-server-lambda" package #1

Closed alexbjorlig closed 3 years ago

alexbjorlig commented 3 years ago

Hi @vibhanshu909 - awesome repo you created, thanks!

I just wanted to hear why you are using the "apollo-server-lambda" package?

In our sveltekit project, we have created a GraphQl endpoint using the apollo-server package.

I have attached the code below. One of the reasons I'm asking is because we can't get it to work with apollo playground, it fails with Must provide query string.. Are we missing something simple, or is that exactly why you are using the apollo-server-lambda package?

mport type { RequestHandler } from '@sveltejs/kit';
import type { EddyLocals } from '../../hooks';
import server from './_server';

const handler: RequestHandler<EddyLocals> = async (args) => {
    const { headers, locals, rawBody } = args;
    let operationName, variables, query, redirectUri;
    if (typeof rawBody === 'string') {
        ({ operationName, variables, query } = JSON.parse(rawBody));
    }
    const response = await server.executeOperation(
        {
            http: operationName,
            variables,
            query: query || '',
        },
        { headers, locals }
    );

    if (response.errors) {
        return {
            status: response?.http?.status,
            body: JSON.stringify(response),
        };
    }

    if (redirectUri) {
        return {
            status: 302,
            headers: {
                Location: redirectUri,
            },
            body: JSON.stringify(response),
        };
    }

    return {
        status: response?.http?.status,
        body: JSON.stringify(response),
        headers: {
            'Content-Type': 'application/json',
        },
    };
};

export const head = handler;
export const get = handler;
export const post = handler;
vibhanshu909 commented 3 years ago

So, basically, the main reason I'm using apollo-server-lambda is that it is built for a serverless environment, whereas apollo-server on the other hand contains features like graphql-subscription, using websockets, which makes it unsuitable for serverless. As for your query "we can't get it to work with apollo playground, it fails with Must provide query string", I can't say anything for sure, until I see a reproducible. Try using https://www.electronjs.org/apps/graphql-playground to connect to the endpoint and see, if the introspection is working.

alexbjorlig commented 3 years ago

Ok, in the end I made it work without apollo-server-lambda, by using the renderPlaygroundPage from @apollographql/graphql-playground-html page. Thanks for the input 👍