Skip to content

A single function that takes GraphQL parameters and does parsing, validation, context assembly and execution/subscription #1493

Open
@enisdenjo

Description

@enisdenjo

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.

Supersedes #1491
Necessary for #1490

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions