typescriptgraphqlcode-generationgraphql-codegen

Unable to find any GraphQL type definitions for the following pointers: src/**/*.graphql


I am using the @graphql-codegen/cli tool to generate typescript types out of my graphql server. Here is my codegen.yml content:

overwrite: true
schema: "http://localhost:3001/graphql"
documents: "src/**/*.graphql"
generates:
  src/generated/graphql.tsx:
    plugins:
      - "typescript"
      - "typescript-operations"
      - "typescript-react-apollo"
  ./graphql.schema.json:
    plugins:
      - "introspection"

Here is the package.json script I use to generate my types (yarn schema):

"schema": "graphql-codegen --config codegen.yml"

All these have been automatically generated by executing the cli wizard yarn codegen init.

But when I run yarn schema, these are the errors I get:

enter image description here

(server is positively running at http://localhost:3001/graphql and exposes the graph schema.

Thanks for your help and suggestion

Here is the .graphql file hosted in my server (http://localhost:3001/graphql

# -----------------------------------------------
# !!! 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!
}

Solution

  • The file your shared is your schema (as generated by your server), but it seems that you haven't created any queries or mutations on top of it. This would be a reason why the codegen is not working properly.

    I suggest thay you create a new file with a simple query, such as: get-users.query.graphql

    query GetUsers {
      user {
        _id
        __typename
        name
        surname
        email
        phone
      }
    }
    

    And add it to your src folder (since your codegen is configured to find all .graphql files inside your src folder). Then re-run the codegen and see if it works well.

    Aftewards you can generated all kinds of .graphql files with your queries and mutations and use the codegen to generate the corresponding types.