Standard Schema for GraphQL response.
npm install --save @dreamit/graphql-std-schema
TypeScript declarations are provided within the project.
- Works with both strings and objects
- Strings can be parsed to objects (see parseStringToObject option)
- Checks/Validates if input is a valid GraphQL response
- At least one of data or errors field exists
- data needs to be an object
- errors needs to be an array
- errors should not be empty is it exists
- extensions should be an object if it exists
- There should be no additional fields besides data, errors and extensions (configurable)
- findIssues function is exported and can be used without generating a schema
- Typescript with zero additional production dependencies
The library exports one type and two functions.
- graphQLResponseSchema: Creates a GraphQL response schema. Options can be provided to adjust validation/checks
- findIssues: Checks for issues in the response object with the given options. Internally used but can also be used standalone to check for issues without generating a Schema.
- ValidationOptions: Interface for the Validation options.
Two options are available to configure validation behavior. See interface below.
export interface ValidationOptions {
/**
* In a GraphQL response only the fields "data", "errors" and "extensions" are allowed. When set to true
* this checks allows additional fields. According to GraphQL spec October 2021 this should not be the case.
*/
allowAdditionalFieldsInResponse?: boolean
/**
* If value is a string, define if it should be parsed to an object. Otherwise an issue will be created.
*/
parseStringToObject?: boolean
}
To validate an input the schema can be generated with graphQLResponseSchema()
and the input can be validated with the
validate function. For a quick check the findIssues function can be used
without a schema, the function will return a StandardSchemaV1.Issue[].
Example code:
// Optionally, provide options to adjust validation
const schema = graphQLResponseSchema()
const validationResults = schema['~standard'].validate(someInput)
// Quick validation without a schema
const foundValidationIssues = findIssues(someInput)
If you have problems, questions or issues please visit our Issue page and open a new issue if there are no fitting issues for your topic yet.
graphql-std-schema is under MIT-License.