graphql#NamedTypeNode TypeScript Examples
The following examples show how to use
graphql#NamedTypeNode.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: generate-query.ts From graphql-query-generator with MIT License | 6 votes |
/**
* Given a type node, return the named type node
*/
function unwrapType(type: TypeNode): NamedTypeNode {
if (type.kind === 'ListType' || type.kind === 'NonNullType') {
return unwrapType(type.type)
} else {
return type
}
}
Example #2
Source File: ts-artifacts.ts From graphql-mesh with MIT License | 6 votes |
function buildSignatureBasedOnRootFields(
codegenHelpers: CodegenHelpers,
type: Maybe<GraphQLObjectType>,
namespace: string
): Record<string, string> {
if (!type) {
return {};
}
const fields = type.getFields();
const operationMap: Record<string, string> = {};
for (const fieldName in fields) {
const field = fields[fieldName];
const argsExists = field.args && field.args.length > 0;
const argsName = argsExists ? `${namespace}.${type.name}${field.name}Args` : '{}';
const parentTypeNode: NamedTypeNode = {
kind: Kind.NAMED_TYPE,
name: {
kind: Kind.NAME,
value: type.name,
},
};
operationMap[fieldName] = ` /** ${field.description} **/\n ${
field.name
}: InContextSdkMethod<${namespace}.${codegenHelpers.getTypeToUse(
parentTypeNode
)}['${fieldName}'], ${argsName}, ${unifiedContextIdentifier}>`;
}
return operationMap;
}
Example #3
Source File: ts-artifacts.ts From graphql-mesh with MIT License | 5 votes |
public getTypeToUse(namedType: NamedTypeNode): string {
if (this.scalars[namedType.name.value]) {
return this._getScalar(namedType.name.value);
}
return this._getTypeForNode(namedType);
}
Example #4
Source File: gqlTypes.ts From ra-data-prisma with MIT License | 5 votes |
nonNullType = (
type: NamedTypeNode | ListTypeNode,
): NonNullTypeNode => ({
kind: Kind.NON_NULL_TYPE,
type,
})
Example #5
Source File: gqlTypes.ts From ra-data-prisma with MIT License | 5 votes |
namedType = (name: NameNode): NamedTypeNode => ({
kind: Kind.NAMED_TYPE,
name,
})
Example #6
Source File: input-name.ts From graphql-eslint with MIT License | 4 votes |
rule: GraphQLESLintRule<[InputNameRuleConfig]> = {
meta: {
type: 'suggestion',
hasSuggestions: true,
docs: {
description:
'Require mutation argument to be always called "input" and input type to be called Mutation name + "Input".\nUsing the same name for all input parameters will make your schemas easier to consume and more predictable. Using the same name as mutation for InputType will make it easier to find mutations that InputType belongs to.',
category: 'Schema',
url: 'https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/input-name.md',
examples: [
{
title: 'Incorrect',
usage: [{ checkInputType: true }],
code: /* GraphQL */ `
type Mutation {
SetMessage(message: InputMessage): String
}
`,
},
{
title: 'Correct (with checkInputType)',
usage: [{ checkInputType: true }],
code: /* GraphQL */ `
type Mutation {
SetMessage(input: SetMessageInput): String
}
`,
},
{
title: 'Correct (without checkInputType)',
usage: [{ checkInputType: false }],
code: /* GraphQL */ `
type Mutation {
SetMessage(input: AnyInputTypeName): String
}
`,
},
],
},
schema: [
{
type: 'object',
additionalProperties: false,
properties: {
checkInputType: {
type: 'boolean',
default: false,
description: 'Check that the input type name follows the convention <mutationName>Input',
},
caseSensitiveInputType: {
type: 'boolean',
default: true,
description: 'Allow for case discrepancies in the input type name',
},
checkQueries: {
type: 'boolean',
default: false,
description: 'Apply the rule to Queries',
},
checkMutations: {
type: 'boolean',
default: true,
description: 'Apply the rule to Mutations',
},
},
},
],
},
create(context) {
const options: InputNameRuleConfig = {
checkInputType: false,
caseSensitiveInputType: true,
checkQueries: false,
checkMutations: true,
...context.options[0],
};
const shouldCheckType = node =>
(options.checkMutations && isMutationType(node)) || (options.checkQueries && isQueryType(node));
const listeners: GraphQLESLintRuleListener = {
'FieldDefinition > InputValueDefinition[name.value!=input] > Name'(node: GraphQLESTreeNode<NameNode>) {
if (shouldCheckType((node as any).parent.parent.parent)) {
const inputName = node.value;
context.report({
node,
message: `Input \`${inputName}\` should be called \`input\`.`,
suggest: [
{
desc: 'Rename to `input`',
fix: fixer => fixer.replaceText(node as any, 'input'),
},
],
});
}
},
};
if (options.checkInputType) {
listeners['FieldDefinition > InputValueDefinition NamedType'] = (node: GraphQLESTreeNode<NamedTypeNode>) => {
const findInputType = item => {
let currentNode = item;
while (currentNode.type !== Kind.INPUT_VALUE_DEFINITION) {
currentNode = currentNode.parent;
}
return currentNode;
};
const inputValueNode = findInputType(node);
if (shouldCheckType(inputValueNode.parent.parent)) {
const mutationName = `${inputValueNode.parent.name.value}Input`;
const name = node.name.value;
if (
(options.caseSensitiveInputType && node.name.value !== mutationName) ||
name.toLowerCase() !== mutationName.toLowerCase()
) {
context.report({
node: node.name,
message: `Input type \`${name}\` name should be \`${mutationName}\`.`,
suggest: [
{
desc: `Rename to \`${mutationName}\``,
fix: fixer => fixer.replaceText(node as any, mutationName),
},
],
});
}
}
};
}
return listeners;
},
}