diff --git a/__fixtures__/scalars/schema.graphql b/__fixtures__/scalars/schema.graphql new file mode 100644 index 0000000000..87f3e90d16 --- /dev/null +++ b/__fixtures__/scalars/schema.graphql @@ -0,0 +1,28 @@ +""" +period of life for mortal deities +period of the life for deities +@typedef {{ __typename: "Lifespan.Limited", max: ?number }} Lifespan_Limited +@type {("Immortal" | Lifespan_Limited) } +""" +scalar Lifespan + +scalar Power + +type God { + name: String! + lifespan: Lifespan! + power: Power +} + +# represents inline variant Deity.Titan +type Deity_Titan { + name: String! +} + +union Deity + = God + | Deity_Titan + +type Query { + deities(lifespan: Lifespan): [Deity!]! +} diff --git a/__fixtures__/scalars/schema.json b/__fixtures__/scalars/schema.json new file mode 100644 index 0000000000..ba9a19e9c0 --- /dev/null +++ b/__fixtures__/scalars/schema.json @@ -0,0 +1,957 @@ +{ + "data": { + "__schema": { + "queryType": { "name": "Query" }, + "mutationType": null, + "subscriptionType": null, + "types": [ + { + "kind": "OBJECT", + "name": "Query", + "description": null, + "fields": [ + { + "name": "deities", + "description": null, + "args": [ + { + "name": "lifespan", + "description": null, + "type": { + "kind": "SCALAR", + "name": "Lifespan", + "ofType": null + }, + "defaultValue": null + } + ], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "UNION", + "name": "Deity", + "ofType": null + } + } + } + }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "SCALAR", + "name": "Lifespan", + "description": "period of life for mortal deities\nperiod of the life for deities\n@typedef {{ __typename: \"Lifespan.Limited\", max: ?number }} Lifespan_Limited\n@type {(\"Immortal\" | Lifespan_Limited) }", + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "UNION", + "name": "Deity", + "description": null, + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": null, + "possibleTypes": [ + { "kind": "OBJECT", "name": "God", "ofType": null }, + { "kind": "OBJECT", "name": "Deity_Titan", "ofType": null } + ] + }, + { + "kind": "OBJECT", + "name": "God", + "description": null, + "fields": [ + { + "name": "name", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "SCALAR", "name": "String", "ofType": null } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "lifespan", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "Lifespan", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "power", + "description": null, + "args": [], + "type": { "kind": "SCALAR", "name": "Power", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "SCALAR", + "name": "String", + "description": "The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.", + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "SCALAR", + "name": "Power", + "description": null, + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "Deity_Titan", + "description": null, + "fields": [ + { + "name": "name", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "SCALAR", "name": "String", "ofType": null } + }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "__Schema", + "description": "A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations.", + "fields": [ + { + "name": "types", + "description": "A list of all types supported by this server.", + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "OBJECT", + "name": "__Type", + "ofType": null + } + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "queryType", + "description": "The type that query operations will be rooted at.", + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "OBJECT", "name": "__Type", "ofType": null } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "mutationType", + "description": "If this server supports mutation, the type that mutation operations will be rooted at.", + "args": [], + "type": { "kind": "OBJECT", "name": "__Type", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "subscriptionType", + "description": "If this server support subscription, the type that subscription operations will be rooted at.", + "args": [], + "type": { "kind": "OBJECT", "name": "__Type", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "directives", + "description": "A list of all directives supported by this server.", + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "OBJECT", + "name": "__Directive", + "ofType": null + } + } + } + }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "__Type", + "description": "The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the `__TypeKind` enum.\n\nDepending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name and description, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types.", + "fields": [ + { + "name": "kind", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "ENUM", + "name": "__TypeKind", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "name", + "description": null, + "args": [], + "type": { "kind": "SCALAR", "name": "String", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "description", + "description": null, + "args": [], + "type": { "kind": "SCALAR", "name": "String", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "fields", + "description": null, + "args": [ + { + "name": "includeDeprecated", + "description": null, + "type": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null + }, + "defaultValue": "false" + } + ], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "OBJECT", + "name": "__Field", + "ofType": null + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "interfaces", + "description": null, + "args": [], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "OBJECT", + "name": "__Type", + "ofType": null + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "possibleTypes", + "description": null, + "args": [], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "OBJECT", + "name": "__Type", + "ofType": null + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "enumValues", + "description": null, + "args": [ + { + "name": "includeDeprecated", + "description": null, + "type": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null + }, + "defaultValue": "false" + } + ], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "OBJECT", + "name": "__EnumValue", + "ofType": null + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "inputFields", + "description": null, + "args": [], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "OBJECT", + "name": "__InputValue", + "ofType": null + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "ofType", + "description": null, + "args": [], + "type": { "kind": "OBJECT", "name": "__Type", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "ENUM", + "name": "__TypeKind", + "description": "An enum describing what kind of type a given `__Type` is.", + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": [ + { + "name": "SCALAR", + "description": "Indicates this type is a scalar.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "OBJECT", + "description": "Indicates this type is an object. `fields` and `interfaces` are valid fields.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "INTERFACE", + "description": "Indicates this type is an interface. `fields` and `possibleTypes` are valid fields.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "UNION", + "description": "Indicates this type is a union. `possibleTypes` is a valid field.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "ENUM", + "description": "Indicates this type is an enum. `enumValues` is a valid field.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "INPUT_OBJECT", + "description": "Indicates this type is an input object. `inputFields` is a valid field.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "LIST", + "description": "Indicates this type is a list. `ofType` is a valid field.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "NON_NULL", + "description": "Indicates this type is a non-null. `ofType` is a valid field.", + "isDeprecated": false, + "deprecationReason": null + } + ], + "possibleTypes": null + }, + { + "kind": "SCALAR", + "name": "Boolean", + "description": "The `Boolean` scalar type represents `true` or `false`.", + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "__Field", + "description": "Object and Interface types are described by a list of Fields, each of which has a name, potentially a list of arguments, and a return type.", + "fields": [ + { + "name": "name", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "SCALAR", "name": "String", "ofType": null } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "description", + "description": null, + "args": [], + "type": { "kind": "SCALAR", "name": "String", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "args", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "OBJECT", + "name": "__InputValue", + "ofType": null + } + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "type", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "OBJECT", "name": "__Type", "ofType": null } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "isDeprecated", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "deprecationReason", + "description": null, + "args": [], + "type": { "kind": "SCALAR", "name": "String", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "__InputValue", + "description": "Arguments provided to Fields or Directives and the input fields of an InputObject are represented as Input Values which describe their type and optionally a default value.", + "fields": [ + { + "name": "name", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "SCALAR", "name": "String", "ofType": null } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "description", + "description": null, + "args": [], + "type": { "kind": "SCALAR", "name": "String", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "type", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "OBJECT", "name": "__Type", "ofType": null } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "defaultValue", + "description": "A GraphQL-formatted string representing the default value for this input value.", + "args": [], + "type": { "kind": "SCALAR", "name": "String", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "__EnumValue", + "description": "One possible value for a given Enum. Enum values are unique values, not a placeholder for a string or numeric value. However an Enum value is returned in a JSON response as a string.", + "fields": [ + { + "name": "name", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "SCALAR", "name": "String", "ofType": null } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "description", + "description": null, + "args": [], + "type": { "kind": "SCALAR", "name": "String", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "isDeprecated", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "deprecationReason", + "description": null, + "args": [], + "type": { "kind": "SCALAR", "name": "String", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "__Directive", + "description": "A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document.\n\nIn some cases, you need to provide options to alter GraphQL's execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor.", + "fields": [ + { + "name": "name", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "SCALAR", "name": "String", "ofType": null } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "description", + "description": null, + "args": [], + "type": { "kind": "SCALAR", "name": "String", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "locations", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "ENUM", + "name": "__DirectiveLocation", + "ofType": null + } + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "args", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "OBJECT", + "name": "__InputValue", + "ofType": null + } + } + } + }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "ENUM", + "name": "__DirectiveLocation", + "description": "A Directive can be adjacent to many parts of the GraphQL language, a __DirectiveLocation describes one such possible adjacencies.", + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": [ + { + "name": "QUERY", + "description": "Location adjacent to a query operation.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "MUTATION", + "description": "Location adjacent to a mutation operation.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "SUBSCRIPTION", + "description": "Location adjacent to a subscription operation.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "FIELD", + "description": "Location adjacent to a field.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "FRAGMENT_DEFINITION", + "description": "Location adjacent to a fragment definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "FRAGMENT_SPREAD", + "description": "Location adjacent to a fragment spread.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "INLINE_FRAGMENT", + "description": "Location adjacent to an inline fragment.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "VARIABLE_DEFINITION", + "description": "Location adjacent to a variable definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "SCHEMA", + "description": "Location adjacent to a schema definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "SCALAR", + "description": "Location adjacent to a scalar definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "OBJECT", + "description": "Location adjacent to an object type definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "FIELD_DEFINITION", + "description": "Location adjacent to a field definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "ARGUMENT_DEFINITION", + "description": "Location adjacent to an argument definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "INTERFACE", + "description": "Location adjacent to an interface definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "UNION", + "description": "Location adjacent to a union definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "ENUM", + "description": "Location adjacent to an enum definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "ENUM_VALUE", + "description": "Location adjacent to an enum value definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "INPUT_OBJECT", + "description": "Location adjacent to an input object type definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "INPUT_FIELD_DEFINITION", + "description": "Location adjacent to an input object field definition.", + "isDeprecated": false, + "deprecationReason": null + } + ], + "possibleTypes": null + } + ], + "directives": [ + { + "name": "skip", + "description": "Directs the executor to skip this field or fragment when the `if` argument is true.", + "locations": ["FIELD", "FRAGMENT_SPREAD", "INLINE_FRAGMENT"], + "args": [ + { + "name": "if", + "description": "Skipped when true.", + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null + } + }, + "defaultValue": null + } + ] + }, + { + "name": "include", + "description": "Directs the executor to include this field or fragment only when the `if` argument is true.", + "locations": ["FIELD", "FRAGMENT_SPREAD", "INLINE_FRAGMENT"], + "args": [ + { + "name": "if", + "description": "Included when true.", + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null + } + }, + "defaultValue": null + } + ] + }, + { + "name": "deprecated", + "description": "Marks an element of a GraphQL schema as no longer supported.", + "locations": ["FIELD_DEFINITION", "ENUM_VALUE"], + "args": [ + { + "name": "reason", + "description": "Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted using the Markdown syntax (as specified by [CommonMark](https://commonmark.org/).", + "type": { "kind": "SCALAR", "name": "String", "ofType": null }, + "defaultValue": "\"No longer supported\"" + } + ] + } + ] + } + } +} diff --git a/package-lock.json b/package-lock.json index 620840cf94..35997f4bbf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5517,6 +5517,12 @@ "@types/node": "*" } }, + "node_modules/@types/doctrine": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/@types/doctrine/-/doctrine-0.0.5.tgz", + "integrity": "sha512-JJwEeFy8Sl9ctiugU4h4DGN9hCB47oyhUkM2H8g8xZr4tHTEXtmV4U6krKrU8Ng0S7RlG/J7fkta1rGu3pq+YQ==", + "dev": true + }, "node_modules/@types/glob": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz", @@ -8473,6 +8479,17 @@ "node": ">=8" } }, + "node_modules/doctrine": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz", + "integrity": "sha512-yS+Q5i3hBf7GBkd4KG8a7eBNNWNGLTaEwwYWUijIYM7zrlYDM0BFXHjjPWlWZ1Rg7UaddZeIDmi9jF3HmqiQ2w==", + "dependencies": { + "esutils": "^2.0.2" + }, + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/dom-serializer": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-1.3.2.tgz", @@ -8954,7 +8971,6 @@ "version": "2.0.2", "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.2.tgz", "integrity": "sha1-Cr9PHKpbyx96nYrMbepPqqBLrJs=", - "dev": true, "engines": { "node": ">=0.10.0" } @@ -20669,8 +20685,12 @@ "apollo-codegen-core": "file:../apollo-codegen-core", "change-case": "^4.0.0", "common-tags": "^1.5.1", + "doctrine": "^3.0.0", "inflected": "^2.0.3" }, + "devDependencies": { + "@types/doctrine": "0.0.5" + }, "engines": { "node": ">=8", "npm": ">=6" @@ -25198,6 +25218,12 @@ "@types/node": "*" } }, + "@types/doctrine": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/@types/doctrine/-/doctrine-0.0.5.tgz", + "integrity": "sha512-JJwEeFy8Sl9ctiugU4h4DGN9hCB47oyhUkM2H8g8xZr4tHTEXtmV4U6krKrU8Ng0S7RlG/J7fkta1rGu3pq+YQ==", + "dev": true + }, "@types/glob": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz", @@ -26165,9 +26191,11 @@ "requires": { "@babel/generator": "7.16.5", "@babel/types": "7.16.0", + "@types/doctrine": "0.0.5", "apollo-codegen-core": "file:../apollo-codegen-core", "change-case": "^4.0.0", "common-tags": "^1.5.1", + "doctrine": "^3.0.0", "inflected": "^2.0.3" }, "dependencies": { @@ -28343,6 +28371,14 @@ } } }, + "doctrine": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz", + "integrity": "sha512-yS+Q5i3hBf7GBkd4KG8a7eBNNWNGLTaEwwYWUijIYM7zrlYDM0BFXHjjPWlWZ1Rg7UaddZeIDmi9jF3HmqiQ2w==", + "requires": { + "esutils": "^2.0.2" + } + }, "dom-serializer": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-1.3.2.tgz", @@ -28698,8 +28734,7 @@ "esutils": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.2.tgz", - "integrity": "sha1-Cr9PHKpbyx96nYrMbepPqqBLrJs=", - "dev": true + "integrity": "sha1-Cr9PHKpbyx96nYrMbepPqqBLrJs=" }, "eventemitter3": { "version": "4.0.7", diff --git a/packages/apollo-codegen-typescript/package.json b/packages/apollo-codegen-typescript/package.json index 4c5dd679ca..d33fb7d237 100644 --- a/packages/apollo-codegen-typescript/package.json +++ b/packages/apollo-codegen-typescript/package.json @@ -22,6 +22,7 @@ "apollo-codegen-core": "file:../apollo-codegen-core", "change-case": "^4.0.0", "common-tags": "^1.5.1", + "doctrine": "^3.0.0", "inflected": "^2.0.3" }, "jest": { @@ -49,5 +50,8 @@ "diagnostics": false } } + }, + "devDependencies": { + "@types/doctrine": "0.0.5" } } diff --git a/packages/apollo-codegen-typescript/src/__tests__/__snapshots__/scalarAnnotations.ts.snap b/packages/apollo-codegen-typescript/src/__tests__/__snapshots__/scalarAnnotations.ts.snap new file mode 100644 index 0000000000..f3346f4f91 --- /dev/null +++ b/packages/apollo-codegen-typescript/src/__tests__/__snapshots__/scalarAnnotations.ts.snap @@ -0,0 +1,64 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Typescript codeGeneration simple hero query 1`] = ` +Object { + "common": "/* tslint:disable */ +/* eslint-disable */ +// @generated +// This file was automatically generated and should not be edited. + +//============================================================== +// START Enums and Input Objects +//============================================================== + +export type Lifespan_Limited = { + __typename: \\"Lifespan.Limited\\"; + max: number | undefined; +}; + +export type Lifespan = \\"Immortal\\" | Lifespan_Limited; + +//============================================================== +// END Enums and Input Objects +//============================================================== +", + "generatedFiles": Array [ + Object { + "content": TypescriptGeneratedFile { + "fileContents": "/* tslint:disable */ +/* eslint-disable */ +// @generated +// This file was automatically generated and should not be edited. + +// ==================================================== +// GraphQL query operation: GetDeities +// ==================================================== + +export interface GetDeities_deities_God { + __typename: \\"God\\"; + name: string; + lifespan: Lifespan; +} + +export interface GetDeities_deities_Deity_Titan { + __typename: \\"Deity_Titan\\"; + name: string; +} + +export type GetDeities_deities = GetDeities_deities_God | GetDeities_deities_Deity_Titan; + +export interface GetDeities { + deities: GetDeities_deities[]; +} + +export interface GetDeitiesVariables { + lifespan?: Lifespan | null; +} +", + }, + "fileName": "GetDeities.ts", + "sourcePath": "GraphQL request", + }, + ], +} +`; diff --git a/packages/apollo-codegen-typescript/src/__tests__/scalarAnnotations.ts b/packages/apollo-codegen-typescript/src/__tests__/scalarAnnotations.ts new file mode 100644 index 0000000000..bed8158d27 --- /dev/null +++ b/packages/apollo-codegen-typescript/src/__tests__/scalarAnnotations.ts @@ -0,0 +1,46 @@ +import { parse } from "graphql"; + +import { loadSchema } from "apollo-codegen-core/lib/loading"; +const schema = loadSchema( + require.resolve("../../../../__fixtures__/scalars/schema.json") +); + +import { + compileToIR, + CompilerOptions, + CompilerContext +} from "apollo-codegen-core/lib/compiler"; + +import { generateSource } from "../codeGeneration"; + +function compile( + source: string, + options: CompilerOptions = { + mergeInFieldsFromFragmentSpreads: true, + addTypename: true + } +): CompilerContext { + const document = parse(source); + return compileToIR(schema, document, options); +} + +describe("Typescript codeGeneration", () => { + test("simple hero query", () => { + const context = compile(` + query GetDeities($lifespan: Lifespan) { + deities(lifespan: $lifespan) { + ... on God { + name + lifespan + } + ... on Deity_Titan { + name + } + } + } + `); + + const output = generateSource(context); + expect(output).toMatchSnapshot(); + }); +}); diff --git a/packages/apollo-codegen-typescript/src/codeGeneration.ts b/packages/apollo-codegen-typescript/src/codeGeneration.ts index ec10a05637..4c86e989be 100644 --- a/packages/apollo-codegen-typescript/src/codeGeneration.ts +++ b/packages/apollo-codegen-typescript/src/codeGeneration.ts @@ -1,7 +1,11 @@ import path from "path"; import * as t from "@babel/types"; import { stripIndent } from "common-tags"; -import { GraphQLEnumType, GraphQLInputObjectType } from "graphql"; +import { + GraphQLEnumType, + GraphQLInputObjectType, + GraphQLScalarType +} from "graphql"; import { CompilerContext, @@ -36,6 +40,11 @@ import { import { GraphQLOutputType, getNullableType } from "graphql"; import { maybePush } from "apollo-codegen-core/lib/utilities/array"; import { unifyPaths } from "apollo-codegen-core/lib/utilities/printing"; +import { + inferGQLScalarAnnotations, + isTypeAnnotatedScalar, + GQLScalarAnnotationType +} from "./scalarAnnotations"; class TypescriptGeneratedFile implements BasicGeneratedFile { fileContents: string; @@ -58,6 +67,11 @@ function printEnumsAndInputObjects( //============================================================== `); + typesUsed + .flatMap(inferGQLScalarAnnotations) + .sort() + .forEach(scalarType => generator.typeAliasForScalarType(scalarType)); + typesUsed .filter(isEnumType) .sort() @@ -242,6 +256,10 @@ export class TypescriptAPIGenerator extends TypescriptGenerator { ); } + public typeAliasForScalarType(scalar: GQLScalarAnnotationType) { + this.printer.enqueue(this.scalarDeclaration(scalar)); + } + public typeAliasForEnumType(enumType: GraphQLEnumType) { this.printer.enqueue(this.enumerationDeclaration(enumType)); } @@ -388,9 +406,8 @@ export class TypescriptAPIGenerator extends TypescriptGenerator { return acc; }; - private isGlobalType = (type: GraphQLType) => { - return isEnumType(type) || isInputObjectType(type); - }; + private isGlobalType = (type: GraphQLType) => + isEnumType(type) || isInputObjectType(type) || isTypeAnnotatedScalar(type); private getUnderlyingType = (type: GraphQLType): GraphQLType => { if (isNonNullType(type)) { diff --git a/packages/apollo-codegen-typescript/src/helpers.ts b/packages/apollo-codegen-typescript/src/helpers.ts index e5760d116d..ec531905ae 100644 --- a/packages/apollo-codegen-typescript/src/helpers.ts +++ b/packages/apollo-codegen-typescript/src/helpers.ts @@ -13,6 +13,7 @@ import { import * as t from "@babel/types"; import { CompilerOptions } from "apollo-codegen-core/lib/compiler"; +import { isTypeAnnotatedScalar } from "./scalarAnnotations"; const DEFAULT_FILE_EXTENSION = "ts"; @@ -46,6 +47,8 @@ export function createTypeFromGraphQLTypeFunction( ? t.TSParenthesizedType(elementType) : elementType ); + } else if (isTypeAnnotatedScalar(graphQLType)) { + return t.TSTypeReference(t.identifier(graphQLType.name)); } else if (isScalarType(graphQLType)) { const builtIn = builtInScalarMap[typeName || graphQLType.name]; if (builtIn != null) { diff --git a/packages/apollo-codegen-typescript/src/language.ts b/packages/apollo-codegen-typescript/src/language.ts index 81e1e0dc6a..0ae1a9c3b7 100644 --- a/packages/apollo-codegen-typescript/src/language.ts +++ b/packages/apollo-codegen-typescript/src/language.ts @@ -9,6 +9,7 @@ import { sortEnumValues } from "apollo-codegen-core/lib/utilities/graphql"; import { createTypeFromGraphQLTypeFunction } from "./helpers"; import * as t from "@babel/types"; +import { GQLScalarAnnotationType } from "./scalarAnnotations"; export type ObjectProperty = { name: string; @@ -28,6 +29,15 @@ export default class TypescriptGenerator { ); } + public scalarDeclaration({ + name, + type + }: GQLScalarAnnotationType): t.ExportNamedDeclaration { + return t.exportNamedDeclaration( + t.TSTypeAliasDeclaration(t.identifier(name), undefined, type) + ); + } + public enumerationDeclaration(type: GraphQLEnumType) { const { name, description } = type; const enumMembers = sortEnumValues(type.getValues()).map(({ value }) => { diff --git a/packages/apollo-codegen-typescript/src/scalarAnnotations.ts b/packages/apollo-codegen-typescript/src/scalarAnnotations.ts new file mode 100644 index 0000000000..67a95340ba --- /dev/null +++ b/packages/apollo-codegen-typescript/src/scalarAnnotations.ts @@ -0,0 +1,97 @@ +import { GraphQLScalarType, GraphQLType, isScalarType } from "graphql"; +import * as doc from "doctrine"; +import * as t from "@babel/types"; + +const isAnonymousType = (t: doc.Tag) => t.title === "type"; + +export const parseGQLScalarJSDOC = (gqlType: GraphQLType): doc.Tag[] => { + if (!isScalarType(gqlType)) { + return []; + } + + const { tags = [], description } = + doc.parse(gqlType.description || "", { unwrap: true }) || {}; + + const [targetType, ...anonymousTypes] = tags.filter(isAnonymousType); + + if (anonymousTypes.length > 0) { + throw new Error("only one anonymous type per annotation is permitted"); + } + + return targetType + ? [ + ...tags.filter(x => !isAnonymousType(x)), + { name: gqlType.name, ...targetType, description } + ] + : []; +}; + +// fixes bug in @types/doctrine +type JSDocType = doc.Type | { type: "StringLiteralType"; value: string }; + +const isNotUndefined = (t: T | undefined): t is T => Boolean(t); + +const inferFieldType = (f: doc.Type): t.TSPropertySignature | undefined => + f.type === "FieldType" && f.value + ? t.TSPropertySignature( + t.identifier(f.key), + t.TSTypeAnnotation(inferJSDocType(f.value)) + ) + : undefined; + +const inferCollectionType = ([type, ...types]: t.TSType[]) => + types.length === 0 ? t.TSArrayType(type) : t.TSTupleType([type, ...types]); + +const inferJSDocType = (docType: JSDocType): t.TSType => { + switch (docType.type) { + case "StringLiteralType": + return t.TSLiteralType(t.stringLiteral(docType.value)); + case "NameExpression": + return t.TSTypeReference(t.identifier(docType.name)); + case "UnionType": + return t.TSUnionType(docType.elements.map(inferJSDocType)); + case "RecordType": + return t.TSTypeLiteral( + docType.fields.map(inferFieldType).filter(isNotUndefined) + ); + case "NonNullableType": + return inferJSDocType(docType.expression); + case "VoidLiteral": + return t.TSVoidKeyword(); + // null | undefined keyword + case "UndefinedLiteral": + case "NullLiteral": + case "NullableLiteral": + return t.TSUndefinedKeyword(); + // nullable Types + case "NullableType": + case "OptionalType": + return t.TSUnionType([ + inferJSDocType(docType.expression), + t.TSUndefinedKeyword() + ]); + + case "ArrayType": + return inferCollectionType(docType.elements.map(inferJSDocType)); + default: + throw new Error(`Unsupported TSDoc Type : ${JSON.stringify(docType)}`); + } +}; + +export type GQLScalarAnnotationType = { + name: string; + type: t.TSType; +}; + +export const inferGQLScalarAnnotations = ( + type: GraphQLType +): GQLScalarAnnotationType[] => + parseGQLScalarJSDOC(type) + .map(({ type, name, description }) => + type && name ? { name, type: inferJSDocType(type) } : undefined + ) + .filter(isNotUndefined); + +export const isTypeAnnotatedScalar = ( + type: GraphQLType +): type is GraphQLScalarType => parseGQLScalarJSDOC(type).length > 0;