How to use the Kind function from graphql

Find comprehensive JavaScript graphql.Kind code examples handpicked from public code repositorys.

graphql.Kind is an enum in the GraphQL.js library that defines the types of syntax tokens that are used in GraphQL queries and schemas.

13
14
15
16
17
18
19
20
21
22
let keep = true;
documentFile.document = (0, graphql_1.visit)(documentFile.document, {
    Directive: (node) => {
        if (node.name.value === 'env') {
            const ifValue = node.arguments?.find((arg) => arg.name.value === 'if')?.value;
            keep = ifValue?.kind === graphql_1.Kind.STRING && typeof process.env[ifValue.value] !== 'undefined';
            // Remove the @env directive, since it is only used here
            return null;
        }
        return undefined;
fork icon32
star icon186
watch icon3

14
15
16
17
18
19
20
21
22
23
        throw new Error(`Value is not formatted as a date ${value}`);
    }
    return value;
};
const parseLiteral = (ast) => {
    if (ast.kind !== graphql_1.Kind.STRING) {
        throw new Error(`Query error: can only parse strings but got ${ast.kind}`);
    }
    return validate(ast.value);
};
fork icon0
star icon0
watch icon1

+ 24 other calls in file

How does graphql.Kind work?

graphql.Kind is an enum in the GraphQL.js library that defines the types of syntax tokens that are used in GraphQL queries and schemas. When you parse a GraphQL query or schema using the graphql.parse method, it generates an abstract syntax tree (AST) that represents the structure of the query or schema. The AST is a collection of nodes, where each node corresponds to a specific syntax token, such as a field, argument, or variable. The graphql.Kind enum defines constants for each type of syntax token that can appear in the AST. For example, graphql.Kind.FIELD represents a field in a GraphQL query, graphql.Kind.ARGUMENT represents an argument to a field or directive, and so on. You can use the graphql.Kind enum to inspect the AST and determine the type of each node. For example, you might use if (node.kind === graphql.Kind.FIELD) to check if a node is a field, or if (node.kind === graphql.Kind.VARIABLE_DEFINITION) to check if a node defines a variable. Overall, graphql.Kind provides a convenient way to work with the AST generated by graphql.parse, allowing you to write code that can analyze, transform, or validate GraphQL queries and schemas.

29
30
31
32
33
34
35
36
37
38
},
parseValue(value) {
    return validate(value);
},
parseLiteral(ast) {
    if (ast.kind === graphql_1.Kind.STRING) {
        return validate(ast.value, ast);
    }
    throw (0, error_js_1.createGraphQLError)(`Account Number can only parse String but got '${ast.kind}'`, {
        nodes: [ast],
fork icon0
star icon0
watch icon1

+ 24 other calls in file

40
41
42
43
44
45
46
47
48
49
    return value;
}
function parseObject(ast) {
    const key = ast.fields[0].value;
    const value = ast.fields[1].value;
    if (ast.fields.length === 2 && key.kind === graphql_1.Kind.STRING && key.value === 'Buffer' && value.kind === graphql_1.Kind.LIST) {
        return global.Buffer.from(value.values.map((astValue) => parseInt(astValue.value)));
    }
    throw (0, error_js_1.createGraphQLError)(`Value is not a JSON representation of Buffer: ${(0, graphql_1.print)(ast)}`, {
        nodes: [ast],
fork icon0
star icon0
watch icon1

+ 53 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const { parse, Kind } = require("graphql");

const query = `
query GetUser($id: ID!) {
user(id: $id) {
id
name
email
}
}
`;

const ast = parse(query);

// Loop through each node in the AST
ast.definitions.forEach((definition) => {
  if (definition.kind === Kind.OPERATION_DEFINITION) {
    // This is an operation definition
    console.log(`Operation: ${definition.operation}`);

    // Loop through each field in the operation
    definition.selectionSet.selections.forEach((selection) => {
      if (selection.kind === Kind.FIELD) {
        // This is a field
        console.log(`Field: ${selection.name.value}`);
      }
    });
  }
});

In this example, we first import the parse and Kind operators from the graphql library. We then define a GraphQL query that retrieves information about a user, using a variable for the user ID. We parse the query using the graphql.parse method, which generates an AST representing the query. We then loop through each node in the AST, checking its kind property to determine what type of node it is. If it's an operation definition, we log the type of operation (query, mutation, or subscription) and loop through each field in the operation, logging the name of each field. The output of this example should be: makefile Copy code

239
240
241
242
243
244
245
246
247
248
  };
}
function getTypeNodeForType(type) {
  if (type instanceof graphql.GraphQLNonNull) {
    return {
      kind: graphql.Kind.NON_NULL_TYPE,
      type: getNamedOrListTypeNodeForType(type.ofType)
    };
  }
  return getNamedOrListTypeNodeForType(type);
fork icon0
star icon0
watch icon1

+ 197 other calls in file

224
225
226
227
228
229
230
231
232
233
234
}


function getNamedOrListTypeNodeForType(type) {
  if (type instanceof graphql.GraphQLList) {
    return {
      kind: graphql.Kind.LIST_TYPE,
      type: getTypeNodeForType(type.ofType)
    };
  }
  return {
fork icon0
star icon0
watch icon0

+ 197 other calls in file

26
27
28
29
30
31
32
33
34
35
        continue;
    }
    collectFields(schema, fragments, variableValues, runtimeType, selection.selectionSet, fields, visitedFragmentNames);
    break;
}
case graphql_1.Kind.FRAGMENT_SPREAD: {
    const fragName = selection.name.value;
    if (visitedFragmentNames.has(fragName) || !shouldIncludeNode(variableValues, selection)) {
        continue;
    }
fork icon0
star icon0
watch icon0

+ 11 other calls in file

28
29
30
31
32
33
34
35
36
37
        }
    }
}
newSelectionSet = selections.length
    ? {
        kind: graphql_1.Kind.SELECTION_SET,
        selections,
    }
    : undefined;
const args = (_a = fieldNodes === null || fieldNodes === void 0 ? void 0 : fieldNodes[0]) === null || _a === void 0 ? void 0 : _a.arguments;
fork icon0
star icon0
watch icon0

+ 20 other calls in file

91
92
93
94
95
96
97
98
99
100
    .filter(t => !hasCircularRef([...ancestors, t], {
    depth: circularReferenceDepth,
}))
    .map(t => {
    return {
        kind: graphql_1.Kind.INLINE_FRAGMENT,
        typeCondition: {
            kind: graphql_1.Kind.NAMED_TYPE,
            name: {
                kind: graphql_1.Kind.NAME,
fork icon0
star icon0
watch icon0

+ 31 other calls in file

114
115
116
117
118
119
120
121
122
123
// This code is so weird because it needs to support GraphQL.js 14
// In GraphQL.js 14 there is no `description` value on schemaNode
schemaNode.description =
    ((_b = (_a = schema.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : schema.description != null)
        ? {
            kind: graphql_1.Kind.STRING,
            value: schema.description,
            block: true,
        }
        : undefined;
fork icon0
star icon0
watch icon0

+ 175 other calls in file

1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
const rootName = getRootTypeName(field.type);
return async (args, query, context) => {
  var _result$errors;
  const selectionSet = graphql$1.parse(`fragment x on ${rootName} {${query}}`).definitions[0].selectionSet;
  const document = {
    kind: graphql$1.Kind.DOCUMENT,
    definitions: [{
      kind: graphql$1.Kind.OPERATION_DEFINITION,
      // OperationTypeNode is an ts enum where the values are 'query' | 'mutation' | 'subscription'
      operation: operation,
fork icon0
star icon0
watch icon0

+ 9 other calls in file

104
105
106
107
108
109
110
111
112
113
const directives = getDirectiveNodes(schema, schema, pathToDirectivesInExtensions);
if (!operationTypes.length && !directives.length) {
    return null;
}
const schemaNode = {
    kind: operationTypes != null ? graphql_1.Kind.SCHEMA_DEFINITION : graphql_1.Kind.SCHEMA_EXTENSION,
    operationTypes,
    // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
    directives: directives,
};
fork icon0
star icon0
watch icon0

+ 175 other calls in file

238
239
240
241
242
243
244
245
        location: pointer,
        rawSDL: common.printSchemaWithDirectives(value, options),
        schema: value,
    };
}
else if ((value === null || value === void 0 ? void 0 : value.kind) === graphql.Kind.DOCUMENT) {
    return {
        location: pointer,
fork icon0
star icon0
watch icon1