Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return (request: GraphQLRequest) => {
const {operationName, query} = request;
const operation =
(operationName && documentToOperation.get(operationName)) ||
Object.values(compile(schema, normalizeDocument(query)).operations)[0];
if (operationName != null) {
documentToOperation.set(operationName, operation);
}
return fillObject(
operation.rootType,
operation.rootType,
// the root type is kind of weird, since there is no "field" that
// would be used in a resolver. For simplicity in the common case
// we just hack this type to make it conform.
[operation as any],
data,
request,
context,
) as Data;
{query, operationName = ''}: Operation,
schema: GraphQLSchema,
) {
if (query == null || operationName == null) {
return data;
}
// For some reason, these documents do not have any details on the source,
// which apollo-codegen depends on for top-level operations. This manually
// adds some hacky references so that they are always at least defined.
query.definitions.forEach(definition => {
(definition as any).loc =
definition.loc || ({source: {name: 'GraphQL request'}} as Location);
});
const ast = compile(schema, query);
const operation = ast.operations[operationName];
return Object.keys(data).reduce(
(all, key) => ({
...all,
[key]: normalizeDataWithField(
data[key],
operation.fields &&
operation.fields.find(({responseName}) => responseName === key),
),
}),
{},
);
}
let schema: GraphQLSchema;
try {
schema = projectConfig.getSchema();
} catch (error) {
throw new Error(
`Error parsing '${projectConfig.schemaPath}':\n\n${error.message.replace(
/Syntax Error.*?\(.*?\) /,
'',
)}`,
);
}
return {
ast: compile(schema, document),
config: projectConfig,
};
}