How to use graphql

Comprehensive graphql code examples:

How to use graphql.isType:

124
125
126
127
128
129
130
131
132
133
    className = _a.className,
    beforeNode = _a.beforeNode,
    afterNode = _a.afterNode,
    showParentName = _a.showParentName,
    isActive = _a.isActive;
var isGraphqlType = graphql_1.isType(type);
var fieldName = showParentName && type.parent ? /*#__PURE__*/React.createElement("span", null, type.parent.name, ".", /*#__PURE__*/React.createElement("b", null, type.name)) : type.name;
return /*#__PURE__*/React.createElement(DocsCategoryItem, {
  active: isActive,
  clickable: clickable,

How to use graphql.introspectionQuery:

1
2
3
4
5
6
7
8
9
10
// https://github.com/NathanRSmith/graphql-visualizer

var _ = require('lodash')
var graphql = require('graphql')

module.exports.query = graphql.introspectionQuery
module.exports.theme = {
  header: {
    invert: false
  },

How to use graphql.GraphQLBoolean:

12
13
14
15
16
17
18
19
20
21
case 'int':
  return graphql.GraphQLInt
case 'integer':
  return graphql.GraphQLInt
case 'tinyint':
  return graphql.GraphQLBoolean
case 'smallint':
  return graphql.GraphQLInt
case 'decimal':
  return graphql.GraphQLString

How to use graphql.isNonNullType:

46
47
48
49
50
51
52
53
54
  }
  return {}
}

function getTypeInfo (graphQLType) {
  const isNonNull = isNonNullType(graphQLType.type)
  const type = isNonNull ? graphQLType.type.ofType : graphQLType.type
  return [type, getNamedType(type), isNonNull]
}

How to use graphql.create:

64
65
66
67
68
69
70
71
72
73
74
  }
}
function assertCreateIsNonNullAllowed(meta, config) {
  var _config$graphql2, _config$graphql2$crea;
  if ((_config$graphql2 = config.graphql) !== null && _config$graphql2 !== void 0 && (_config$graphql2$crea = _config$graphql2.create) !== null && _config$graphql2$crea !== void 0 && _config$graphql2$crea.isNonNull && hasCreateAccessControl(config.access)) {
    throw new Error(`The field at ${meta.listKey}.${meta.fieldKey} sets graphql.create.isNonNull: true and has create access control, this is not allowed.\n` + 'Either disable graphql.create.isNonNull or create access control.');
  }
}


// yes, these two types have the fields but they're semantically different types

How to use graphql.typeFromAST:

47
48
49
50
51
52
53
54
55
56

if (variableDefinitions) {
  variableDefinitions.forEach(function (_a) {
    var variable = _a.variable,
        type = _a.type;
    var inputType = graphql_1.typeFromAST(schema, type);

    if (inputType) {
      variableToType[variable.name.value] = inputType;
    }

How to use graphql.isScalarType:

10
11
12
13
14
15
16
17
18
19

function isScalarWithTypePolicy(f) {
  let type = f.type
  if (isNonNullType(type)) type = type.ofType

  return isScalarType(type) && scalarTypePolicies[type.name] !== undefined
}

const content = await code`
  export default {

How to use graphql.visit:

105
106
107
108
109
110
111
112
113
114
    throw new Error(`Duplicate definition of '${name}'.`);
  }
  continue;
}
requiredDefinitions.set(name, definition);
visit(definition, {
  FragmentSpread(spread: FragmentSpreadNode) {
    const baseDefinition = baseMap.get(spread.name.value);
    if (baseDefinition) {
      // We only need to add those definitions not already included

How to use graphql.assertObjectType:

626
627
628
629
630
631
632
633
634
635
    throw new Error(msg);
  }
}
function getSecretFieldImpl(schema, listKey, fieldKey) {
  var _gqlOutputType$getFie, _gqlOutputType$getFie2;
  const gqlOutputType = graphql.assertObjectType(schema.getType(listKey));
  const secretFieldImpl = (_gqlOutputType$getFie = gqlOutputType.getFields()) === null || _gqlOutputType$getFie === void 0 ? void 0 : (_gqlOutputType$getFie2 = _gqlOutputType$getFie[fieldKey].extensions) === null || _gqlOutputType$getFie2 === void 0 ? void 0 : _gqlOutputType$getFie2.keystoneSecretField;
  assertSecretFieldImpl(secretFieldImpl, listKey, fieldKey);
  return secretFieldImpl;
}

How to use graphql.GraphQLInterfaceType:

0
1
2
3
4
5
6
7
8
9
const graphql = require('graphql');
const assert = require('assert');
const _ = require('lodash');

const InputObjectType = graphql.GraphQLInputObjectType;
const InterfaceType = graphql.GraphQLInterfaceType;
const ScalarType = graphql.GraphQLScalarType;
const ObjectType = graphql.GraphQLObjectType;
const UnionType = graphql.GraphQLUnionType;
const NonNullType = graphql.GraphQLNonNull;

How to use graphql.read:

57
58
59
60
61
62
63
64
65
66
  if ((_config$graphql = config.graphql) !== null && _config$graphql !== void 0 && (_config$graphql$read = _config$graphql.read) !== null && _config$graphql$read !== void 0 && _config$graphql$read.isNonNull) {
    if (resolvedIsNullable) {
      throw new Error(`The field at ${meta.listKey}.${meta.fieldKey} sets graphql.read.isNonNull: true but not validation.isRequired: true or db.isNullable: false.\n` + `Set validation.isRequired: true or db.isNullable: false or disable graphql.read.isNonNull`);
    }
    if (hasReadAccessControl(config.access)) {
      throw new Error(`The field at ${meta.listKey}.${meta.fieldKey} sets graphql.read.isNonNull: true and has read access control, this is not allowed.\n` + 'Either disable graphql.read.isNonNull or read access control.');
    }
  }
}
function assertCreateIsNonNullAllowed(meta, config) {

How to use graphql.GraphQLFloat:

32
33
34
35
36
37
38
39
40
41
case 'text':
  return graphql.GraphQLString
case 'bool':
  return graphql.GraphQLBoolean
case 'real':
  return graphql.GraphQLFloat
case 'float8':
  return graphql.GraphQLFloat
case 'double':
  return graphql.GraphQLFloat

How to use graphql.extendSchema:

7
8
9
10
11
12
13
14
15
16
  let astDocument = parse(typeDefs);
  let schema = buildASTSchema(astDocument);

  let extensionsAst = extractExtensionDefinitions(astDocument);
  if (extensionsAst.definitions.length > 0) {
    schema = extendSchema(schema, extensionsAst);
  }

  return schema;
};

How to use graphql.isEnumType:

34
35
36
37
38
39
40
41
42
43
    : [];
const subFragments =
  graphql_1.isInterfaceType(fieldType) || graphql_1.isUnionType(fieldType) ? schema.getPossibleTypes(fieldType) : {};
if (
  (depth < MAX_DEPTH && options.shallow && isConnectionType(fieldType)) ||
  (depth < 1 && !(graphql_1.isScalarType(fieldType) || graphql_1.isEnumType(fieldType)))
) {
  return;
}
const fields = Object.keys(subFields)

How to use graphql.isInterfaceType:

28
29
30
31
32
33
34
35
36
37
// Is exposed by more than one service
return getDuplicateTypes(allTypes)
  .filter(type => {
    // Has to be object, interface or input
    const isEligibleType =
      isObjectType(type) || isInterfaceType(type) || isInputType(type)
    // Is not an entity
    const isNotEntity = !type.astNode.directives.find(
      directive => directive.name.value === 'key'
    )

How to use graphql.getNamedType:

48
49
50
51
52
53
54
55
56
57
}

function getTypeInfo (graphQLType) {
  const isNonNull = isNonNullType(graphQLType.type)
  const type = isNonNull ? graphQLType.type.ofType : graphQLType.type
  return [type, getNamedType(type), isNonNull]
}

module.exports = {
  validateOpts,

How to use graphql.GraphQLInt:

8
9
10
11
12
13
14
15
16
17
function sqlTypeToGraphQL (sqlType) {
  // TODO support more types
  /* istanbul ignore next */
  switch (sqlType) {
    case 'int':
      return graphql.GraphQLInt
    case 'integer':
      return graphql.GraphQLInt
    case 'tinyint':
      return graphql.GraphQLBoolean

How to use graphql.isInputObjectType:

1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
}
const whereInputFields = list.types.where.graphQLType.getFields();
for (const fieldKey of Object.keys(list.fields)) {
  var _whereInputFields$fie, _fieldFilterFields$co;
  const filterType = (_whereInputFields$fie = whereInputFields[fieldKey]) === null || _whereInputFields$fie === void 0 ? void 0 : _whereInputFields$fie.type;
  const fieldFilterFields = graphql.isInputObjectType(filterType) ? filterType.getFields() : undefined;
  if ((fieldFilterFields === null || fieldFilterFields === void 0 ? void 0 : (_fieldFilterFields$co = fieldFilterFields.contains) === null || _fieldFilterFields$co === void 0 ? void 0 : _fieldFilterFields$co.type) === graphql.GraphQLString) {
    var _fieldFilterFields$mo;
    searchableFields.set(fieldKey, (fieldFilterFields === null || fieldFilterFields === void 0 ? void 0 : (_fieldFilterFields$mo = fieldFilterFields.mode) === null || _fieldFilterFields$mo === void 0 ? void 0 : _fieldFilterFields$mo.type) === nextFields.QueryMode.graphQLType ? 'insensitive' : 'default');
  }

How to use graphql.astFromValue:

258
259
260
261
262
263
264
265
266
267
      name: {
        kind: graphql.Kind.NAME,
        value: arg.name
      }
    },
    defaultValue: arg.defaultValue === undefined ? undefined : (_ref = graphql.astFromValue(arg.defaultValue, arg.type)) !== null && _ref !== void 0 ? _ref : undefined
  };
});
const argumentNodes = field.args.map(arg => ({
  kind: graphql.Kind.ARGUMENT,

How to use graphql.OperationTypeNode:

339
340
341
342
343
344
345
346
347
348
} = getVariablesForGraphQLField(field);
const document = {
  kind: graphql.Kind.DOCUMENT,
  definitions: [{
    kind: graphql.Kind.OPERATION_DEFINITION,
    operation: graphql.OperationTypeNode.QUERY,
    selectionSet: {
      kind: graphql.Kind.SELECTION_SET,
      selections: [{
        kind: graphql.Kind.FIELD,

How to use graphql.isEnabled:

1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
}
if (idField.input.update !== undefined) {
  throw new Error(`The idField on a list must not define an update GraphQL input but the idField for ${list.listKey} does define one`);
}
if (idField.graphql.isEnabled.read === false) {
  throw new Error(`The idField on a list must not have graphql.isEnabled.read be set to false but ${list.listKey} does`);
}
if (idField.output.type.kind !== 'non-null' || idField.output.type.of !== apiWithoutContext.ID) {
  throw new Error(`The idField on a list must define a GraphQL output field with a non-nullable ID GraphQL scalar type but the idField for ${list.listKey} defines the type ${idField.output.type.graphQLType.toString()}`);
}

How to use graphql.getIntrospectionQuery:

1
2
3
4
5
6
7
8
9
10
// https://github.com/NathanRSmith/graphql-visualizer

var _ = require('lodash')
var graphql = require('graphql')

module.exports.query = graphql.getIntrospectionQuery()
module.exports.theme = {
  header: {
    invert: false
  },

How to use graphql.validate:

33
34
35
36
37
38
39
40
41
42
43
44
  }
}


const customValidateFn = (...args) => {
  // Identify validation errors so that customFormatErrorFn will allow them to be returned to the user.
  const validationErrors = validate(...args)


  return validationErrors.map(
    (error) =>
      new GraphQLError(

How to use graphql.validateSchema:

226
227
228
229
230
231
232
233
234
235
}
if (nextProps.configString !== this.props.configString) {
    this.props.setConfigString(nextProps.configString);
}
if (nextProps.schema !== this.props.schema) {
    var validationErrors = graphql_1.validateSchema(nextProps.schema);
    if (validationErrors && validationErrors.length > 0) {
        throw new InvalidSchemaError_1.InvalidSchemaError(validationErrors);
    }
    this.setState({ schema: nextProps.schema });

How to use graphql.getOperationAST:

125
126
127
128
129
130
131
132
133
134
customParseFn,
customValidateFn,
customExecuteFn: async (args) => {
  const { document, operationName, variableValues } = args

  const operationAST = getOperationAST(document, operationName)
  if (!operationAST) {
    return {
      errors: [
        new GraphQLError(

How to use graphql.Source:

147
148
149
150
151
152
153
154
155
  hello () {
    return Promise.resolve('Hello world!')
  }
}
var query = '{ hello }'
var source = new graphql.Source(query)
var documentAST = graphql.parse(source)

agent.startTransaction('foo')

How to use graphql.buildASTSchema:

76
77
78
79
80
81
82
83
84
85
// given a "GraphQL schema language" text file, converts into introspection JSON
function introspect (text) {
  return new Promise(function (resolve, reject) {
    try {
      var astDocument = GraphQL.parse(text)
      var schema = GraphQL.buildASTSchema(astDocument)
      GraphQL.graphql({ schema, source: graphqlviz.query })
        .then(function (data) {
          resolve(data)
        })

How to use graphql.buildClientSchema:

62
63
64
65
66
67
68
69
70
  ? getSchemaIntrospection(schemaReference)
  : schemaReference;
if (introspection.__schema) {
  return buildClientSchema((introspection: any));
} else if (introspection.data && introspection.data.__schema) {
  return buildClientSchema((introspection.data: any));
} else if (introspection.kind && introspection.kind === 'Document') {
  return buildASTSchema(introspection);
}

How to use graphql.printSchema:

692
693
694
695
696
697
698
699
700
701
	this.buildLoaderOptionMap(services); // rebuild the options for DataLoaders

	this.shouldUpdateGraphqlSchema = false;

	this.broker.broadcast("graphql.schema.updated", {
		schema: GraphQL.printSchema(schema),
	});
} catch (err) {
	this.logger.error(err);
	throw err;

How to use graphql.GraphQLString:

16
17
18
19
20
21
22
23
24
25
case 'tinyint':
  return graphql.GraphQLBoolean
case 'smallint':
  return graphql.GraphQLInt
case 'decimal':
  return graphql.GraphQLString
case 'bigint':
  return graphql.GraphQLString
case 'int2':
  return graphql.GraphQLInt