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,
};
}
return generate(t.file(t.program(fileBody), [], [])).code;
}
const context = new OperationContext(operation, ast, options, file);
const partialContext = new OperationContext(
operation,
ast,
{...options, partial: true},
file,
);
let rootType: GraphQLObjectType;
if (operation.operationType === OperationType.Query) {
rootType = ast.schema.getQueryType() as any;
} else if (operation.operationType === OperationType.Mutation) {
rootType = ast.schema.getMutationType() as any;
} else {
rootType = ast.schema.getSubscriptionType() as any;
}
const variables =
operation.variables.filter(isTypedVariable).length > 0
? context.export(variablesInterface(operation.variables, context))
: null;
const operationInterface = t.tsInterfaceDeclaration(
t.identifier(context.typeName),
null,
null,
tsInterfaceBodyForObjectField(
operation,
}
return generate(t.file(t.program(fileBody), [], [])).code;
}
const context = new OperationContext(operation, ast, options, file);
const partialContext = new OperationContext(
operation,
ast,
{...options, partial: true},
file,
);
let rootType: GraphQLObjectType;
if (operation.operationType === OperationType.Query) {
rootType = ast.schema.getQueryType() as any;
} else if (operation.operationType === OperationType.Mutation) {
rootType = ast.schema.getMutationType() as any;
} else {
rootType = ast.schema.getSubscriptionType() as any;
}
const variables =
operation.variables.filter(isTypedVariable).length > 0
? context.export(variablesInterface(operation.variables, context))
: null;
const operationInterface = t.tsInterfaceDeclaration(
t.identifier(context.typeName),
null,
null,
get typeName() {
let typeName: string;
if (isOperation(this.operation)) {
const {operationName, operationType} = this.operation;
typeName = `${ucFirst(operationName)}${ucFirst(operationType)}Data`;
} else {
const {fragmentName} = this.operation;
typeName = `${ucFirst(fragmentName)}FragmentData`;
}
return this.options.partial
? typeName.replace(/Data$/, 'PartialData')
: typeName;
}