Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
enisdenjo opened this issue Aug 19, 2022 · 0 comments · May be fixed by #1515
Assignees
Labels
kind/enhancement New feature or request
Milestone

Comments

@enisdenjo
Copy link
Collaborator

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

@enisdenjo enisdenjo changed the title A single function that takes GraphQL parameters and does parsing, validation and execution/subscription A single function that takes GraphQL parameters and does parsing, validation, context assembly and execution/subscription Aug 19, 2022
@ardatan ardatan added this to the v3 milestone Sep 6, 2022
@YassinEldeeb YassinEldeeb added the kind/enhancement New feature or request label Dec 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request
Projects
None yet
3 participants