S
SolidJS12mo ago
regbit

Solid-Start + GraphQL session info

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;
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;
2 Replies
regbit
regbit12mo ago
I've read this (https://start.solidjs.com/core-concepts/api-routes#session-management) once again and decided to output cookies from requests
SolidStart Beta Docuentation
SolidStart Beta Documentation
Early release documentation and resources for SolidStart Beta
regbit
regbit12mo ago
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/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/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)
}
};
// /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)
}
};
Right now as a workaround I passed event from createServerData$ down to gqlCall to pull cookies from there for the fetch and it works. But still feels like I'm missing something.