Open
Description
Envelop is currently split into parts and requires the user to integrate them in order every time they want to use envelop and have a valid GraphQL execution chain.
In essence, the redundancy is:
const { parse, validate, contextFactory, execute, schema } = getEnveloped({ req })
const { query, variables } = JSON.parse(payload)
try {
const document = parse(query)
} catch (err) {
return { errors: [err] }
}
const validationErrors = validate(schema, document)
if (validationErrors.length > 0) {
return { errors: validationErrors }
}
const context = await contextFactory(req)
const result = await execute({
document,
schema,
variableValues: variables,
contextValue: context
})
return result
In order to properly process a GraphQL request, you'll always have to parse, validate and execute/subscribe; with this in mind, I suggest a single, unified, perform
function that does all of the above:
const { perform } = getEnveloped({ req });
const { operationName, query, variables, extensions } = JSON.parse(payload);
const result = await perform({ operationName, query, variables, extensions });
return JSON.stringify(result);
This approach, not only simplifies the usage of envelop, but additionally allows manipulation of the final result through an accompanying onPerform
hook (including parsing and validation errors) to inject implementor specific requirements - like the extensions
field for Apollo's FTV1 tracing.