Describe the bug
I have created a brand new react-native application and I, now, attempt to generate graphql schema from my backend https://__server__url__/graphql where it is exposed.
I entered
$ graphql-codegen init
and followed the steps from the wizard, which produced the below codegen.yml:
overwrite: true
schema: "https://__server__url__/graphql"
documents: "src/**/*.graphql"
generates:
src/generated/graphql.tsx:
plugins:
- "typescript"
- "typescript-operations"
- "typescript-react-apollo"
./graphql.schema.json:
plugins:
- "introspection"
This is the package.json script I should run to generate the schema:
"schema": "graphql-codegen --config codegen.yml"
But the above command fails with the following errors:
✖src/generated/graphql.tsx
Error: Unable to find any GraphQL type definitions for the following pointers: -"src/**/*.graphql"
...
✖ ./graphql.schema.json
Error: Unable to find any GraphQL type definitions for the following pointers: -"src/**/*.graphql"
My backend is a nestjs application which exposes a graphql interface. My schema.gql file has been generated by type-graphql which I paste below
# -----------------------------------------------
# !!! THIS FILE WAS GENERATED BY TYPE-GRAPHQL !!!
# !!! DO NOT MODIFY THIS FILE BY YOURSELF !!!
# -----------------------------------------------
"""Date custom scalar type"""
scalar Date
type Mutation {
create_user(user: UserInput!): User!
create_pofficer(pofficer: POfficerCreateInput!): POfficer!
create_incident(incident: TIncidentInput!): TIncident!
add_incident_type(incident_type: TIncidentTypeInput!): TIncidentType!
}
type POfficer {
_id: ID!
userid: ID!
user: User!
}
input POfficerCreateInput {
name: String!
surname: String!
phone: String!
}
type Query {
users: [User!]!
pofficers: [POfficer!]!
incidents: [TIncident!]!
incident_types: [TIncidentType!]!
}
type TIncident {
_id: ID!
createdAt: Date!
incidenttype_id: ID!
pofficer_id: ID!
toffender_id: ID
toffender_phone: String!
carnumber: String!
incident_status: String!
pofficer: POfficer!
toffender: User!
incident_type: TIncidentType!
}
input TIncidentInput {
incidenttype_id: ID!
pofficer_id: ID!
toffender_phone: String!
carnumber: String!
}
type TIncidentType {
_id: ID!
name: String!
description: String
}
input TIncidentTypeInput {
name: String!
description: String
}
type User {
_id: ID!
name: String!
surname: String!
email: String
phone: String!
}
input UserInput {
name: String!
surname: String!
email: String!
phone: String!
}
# Put your operations here
codegen.yml config file:overwrite: true
schema: "https://__server__url__/graphql"
documents: "src/**/*.graphql"
generates:
src/generated/graphql.tsx:
plugins:
- "typescript"
- "typescript-operations"
- "typescript-react-apollo"
./graphql.schema.json:
plugins:
- "introspection"
Expected behavior
Should generate typescript types from remote schema
Environment:
@graphql-codegen/cli: 1.8.3 Do you have client-side operations on documents: "src/**/*.graphql"?
I have the same problem, operations should be generated from schema right?
No you need to provide the operations against the schema.
I have solved the issue by applying the answer from this stackoverflow question here (the question is also from me)
Basically I had to create a .graphql file with at least 1 operation (query or mutation). And now it works. I don't know whether this is the real solution to this problem or a hack. Maybe it should be mentioned in the tutorial.
Thanks @seyaobey !
I've used amplify-graphql-docs-generator (https://github.com/aws-amplify/amplify-cli/tree/master/packages/amplify-graphql-docs-generator) to generate queries, mutations, etc directly from the type schema.
Is it possible to do this with graqhql-codegen?
@aldegoeij we have an open issue for that: https://github.com/dotansimha/graphql-code-generator/issues/2349
Closing this for now. As mentioned before, either remove typescript-operations and documents: section, or add an actual operation :)
I have the same problem, operations should be generated from schema right?
How to do it
Operations are not generated from the schema. They should be written based on the components / client that needs them.
Generating GraphQL operations isn't different than using REST...
@syedahmedusman2 please refer to: https://github.com/dotansimha/graphql-code-generator/discussions/2349
Most helpful comment
I have solved the issue by applying the answer from this stackoverflow question here (the question is also from me)
Basically I had to create a
.graphqlfile with at least 1 operation (queryormutation). And now it works. I don't know whether this is the real solution to this problem or a hack. Maybe it should be mentioned in the tutorial.