Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
schema,
mocks,
});
const { query, variables, operationName } = JSON.parse(req.body());
const rootValue = getRootValue(operations, operationName, variables);
let response;
try {
response = await graphql({
schema,
source: query,
variableValues: variables,
rootValue,
});
} catch (error) {
// https://github.com/apollographql/apollo-server/blob/61e69412c0aa88b358a2eef4ba635e8618db9946/packages/apollo-server-core/src/runHttpQuery.ts#L295-L297
response = { error: formatApolloErrors([error]) };
}
return res.status(200).body(JSON.stringify(response));
};
const handleProxyGraphQLRequest = async (req, res) => {
(): Promise => {
// Parse the document.
let documentAST: DocumentNode;
if (options.parsedQuery) {
documentAST = options.parsedQuery;
} else if (!options.queryString) {
throw new Error('Must supply one of queryString and parsedQuery');
} else {
const parsingDidEnd = extensionStack.parsingDidStart({
queryString: options.queryString,
});
let graphqlParseErrors: SyntaxError[] | undefined;
try {
documentAST = parse(options.queryString);
} catch (syntaxError) {
graphqlParseErrors = formatApolloErrors(
[
fromGraphQLError(syntaxError, {
errorClass: SyntaxError,
}),
],
{
debug,
},
);
return Promise.resolve({ errors: graphqlParseErrors });
} finally {
parsingDidEnd(...(graphqlParseErrors || []));
}
}
if (
(): Promise => {
// Parse the document.
let documentAST: DocumentNode;
if (options.parsedQuery) {
documentAST = options.parsedQuery;
} else if (!options.queryString) {
throw new Error('Must supply one of queryString and parsedQuery');
} else {
const parsingDidEnd = extensionStack.parsingDidStart({
queryString: options.queryString,
});
let graphqlParseErrors: SyntaxError[] | undefined;
try {
documentAST = parse(options.queryString);
} catch (syntaxError) {
graphqlParseErrors = formatApolloErrors(
[
fromGraphQLError(syntaxError, {
errorClass: SyntaxError,
}),
],
{
debug,
},
);
return Promise.resolve({ errors: graphqlParseErrors });
} finally {
parsingDidEnd(...(graphqlParseErrors || []));
}
}
if (
persistedQueryRegister,
};
return runQuery(params);
} catch (e) {
// Populate any HttpQueryError to our handler which should
// convert it to Http Error.
if (e.name === 'HttpQueryError') {
// async function wraps this in a Promise
throw e;
}
// This error will be uncaught, so we need to wrap it and treat it as an
// internal server error
return {
errors: formatApolloErrors([e], optionsObject),
};
}
}) as Array }>>;
const defaultHeaders = { 'Content-Type': 'application/json' };
// force no-cache on PersistedQuery errors
const headers = hasPersistedQueryError(errors)
? {
...defaultHeaders,
'Cache-Control': 'private, no-cache, must-revalidate',
}
: defaultHeaders;
type Result =
& Pick
& { errors: E[] | ApolloError[] }
const result: Result = {
errors: options
? formatApolloErrors(errors, {
debug: options.debug,
formatter: options.formatError,
})
: errors,
};
if (extensions) {
result.extensions = extensions;
}
throw new HttpQueryError(
statusCode,
prettyJSONStringify(result),
true,
headers,
);
connection.formatResponse = (value: ExecutionResult) => ({
...value,
errors:
value.errors &&
formatApolloErrors([...value.errors], {
formatter: this.requestOptions.formatError,
debug: this.requestOptions.debug,
}),
});
function throwHttpGraphQLError(
statusCode: number,
errors: Array,
optionsObject?: Partial,
): never {
throw new HttpQueryError(
statusCode,
prettyJSONStringify({
errors: optionsObject
? formatApolloErrors(errors, {
debug: optionsObject.debug,
formatter: optionsObject.formatError,
})
: errors,
}),
true,
{
'Content-Type': 'application/json',
},
);
}
onOperation: async (message, connection) => {
connection.formatResponse = (value) => ({
...value,
errors:
value.errors &&
formatApolloErrors([...value.errors], {
formatter: apolloServer.requestOptions.formatError,
debug: apolloServer.requestOptions.debug
})
});
let context;
try {
context = await apolloServer.context({ connection, payload: message.payload });
} catch (error) {
throw formatApolloErrors([error], {
formatter: apolloServer.requestOptions.formatError,
debug: apolloServer.requestOptions.debug
})[0];
}
return { ...connection, context };
}
},
connection.formatResponse = (value: ExecutionResult) => ({
...value,
errors:
value.errors &&
formatApolloErrors([...value.errors], {
formatter: this.requestOptions.formatError,
debug: this.requestOptions.debug,
}),
});
let context: Context = this.context ? this.context : { connection };
function formatErrors(
errors: ReadonlyArray,
): ReadonlyArray {
return formatApolloErrors(errors, {
formatter: config.formatError,
debug: requestContext.debug,
});
}