graphql-hive / graphql-hive/envelop
A single function that takes GraphQL parameters and does parsing, validation, context assembly and execution/subscription
- Dominant language
- No language data
- Stars
- 827
- Forks
- 132
- PR merge metrics
- No merged PRs in 30d
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:
```js
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:
```js
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
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.