is-graphql-document v0.0.2
is-graphql-document (deprecated)
(deprecated) I really think that you do not need this. ;)
Simple tool to check if given structure is graphQL document.
Install: npm install is-graphql-document
isGraphQLDocument
import isGraphQLDocument from 'is-graphql-document';
const query = gql`
{
happyFace(id: 42) {
face
happiness
}
}
`;
isGraphQLDocument(query); // true
isGraphQLDocument('something else'); // false
isGraphQLDocument('{ happyFace(id: 42) { face happiness }}'); // false
isGraphQLDocument(gql`{ happyFace(id: 42) { face happiness }}`); // true
isGraphQLDocumentStrict
Additionally you can use extended version that thoroughly check the object, but requires the whole graphql library, therefore it will add significantly to your bundle. Use specific import here.
import isGraphQLDocumentStrict from 'is-graphql-document/isGraphQLDocumentStrict';
const notGraphQLDoc = { kind: 'Document', definitions: [], loc: { start: 1, end: 1 } };
isValidGraphQlString(string); // true
isValidGraphQlString(stringInvalid); // false
Caveats
To make it fast we suppose that your input that is checked will be reasonably different from standard graphql document schema. This means that this:
{ kind: 'Document', definitions: [], loc: { start: 1, end: 1 } }
will be considered as graphQL document, if you will use standard version. In most cases it's sufficient. If not use strict version.
Feedback
Feedback and PRs are welcome. Please, take into account that the main reason is to have very lightweight tool with minimal (default) dependencies.