#Solid-Start + GraphQL session info

4 messages · Page 1 of 1 (latest)

slow plover
#

Hi!
I have a GraphQL handler at http://localhost:3000/graphql and I want to get session info by calling getSession inside of it but when I do it I receive no data (I do get session details when I call it on pages with createServerData$).

Is it expected behavior? If not, what are the ways to debug it (I'm somewhat new to this)?

File at /src/routes/graphql.tsx

import { GraphQLError, graphql } from 'graphql';
import { APIEvent, json } from 'solid-start';
import { getSession } from '@solid-auth/base';
import { authOptions } from '~/server/auth';
import { rootValue, schema } from '~/lib/graphql/graphql';

import logger from "../utils/logger";

const graphQLHandler = async (event: APIEvent) => {
    // get session
    const session = await getSession(event.request, authOptions);

    // return null ;(
    console.log('session', session); 

    // get request body
    const body = await new Response(event.request.body).json();

    // pass query and save results
    const result = await graphql({ 
        rootValue, 
        schema, 
        source: body.query, 
        variableValues: body.variables, 
        contextValue: { user: session?.user } 
    });

    if(result.errors! && result.errors.length > 0) {
        const error = result.errors[0] as GraphQLError;
        logger.error(error);
        throw error;
    };

    // send query results as response
    return json(result);
};

export const GET = graphQLHandler;
export const POST = graphQLHandler;
slow plover
#

Solid-Start + GraphQL session info

slow plover
#

Maybe I'm calling GQL fetch incorrectly?
Right now the call path looks like this:

// /src/routes/decisions/(decisions).tsx
...
export function routeData() {
    return createServerData$(
        async () => {
            return await getUserDecisionsGQL();
        },
    );
};
...

->

// /src/db/requests/decision.tsx
...
export const getUserDecisionsGQL = async function () {
    const res = await gqlCall(GET_USER_DECISIONS);
    return res.data.getUserDecisions as Decision[];
};
...

->

// /src/lib/actions.tsx
export async function gqlCall(query: string, variables?: any) {
    // make graphql query
    try {
        const response = await fetch("http://localhost:3000/graphql", {
            method: "POST",
            body: JSON.stringify({ query, variables }),
        });

        if (response?.ok) {
            // turn response into javascript object
            const gqlresponse = await response.json();
        
            // return response
            return gqlresponse;
        } else {
            console.log(`HTTP Response Code: ${response?.status}`)
            // throw 
        }
    } catch (error) {
        console.error('error:', error)
    }
};