dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.86k stars 1.33k forks source link

Prisma GrapQL (prisma.io) #715

Closed Industrial closed 5 years ago

Industrial commented 6 years ago

Hi!

I am using Prisma to serve GraphQL. It uses annotations on top of graphql. It also uses some types like DateTime and ID.

I tried this NPM script to take out the annotations:

    "generate:domain": "sed -e \"s/ @.\\+ {/ {/;s/ @.\\+\\$//\" common/domain/datamodel.graphql > common/domain/datamodel-no-annotations.gen.graphql",

But that only takes out the annotations, I still don't know what to do with the types:

Right now when I run gql-gen --schema ./common/domain/datamodel-no-annotations.gen.graphql --template graphql-codegen-typescript-apollo-template --out ./common/domain/entities I get errors like error: Field timestamp: Couldn't find type DateTime in any of the schemas. or error: Cannot read property 'ID' of undefined.

dotansimha commented 6 years ago

Thanks @Industrial! Can you please share your schema (json/file)? Or a repository with a reproduction? thanks!

kamilkisiela commented 6 years ago

@dotansimha I guess the ./common/domain/datamodel.graphql looks something like this (took from here):

type Post {
  id: ID! @unique
  createdAt: DateTime!
  updatedAt: DateTime!
  isPublished: Boolean! @default(value: "false")
  title: String!
  content: String!
  author: User!
}

type User {
  id: ID! @unique
  name: String!
  email: String! @unique
  posts: [Post!]!
  createdAt: DateTime!
  updatedAt: DateTime!
}

I think @Industrial removes directives and what's left is:

type Post {
  id: ID!
  createdAt: DateTime!
  updatedAt: DateTime!
  isPublished: Boolean!
  title: String!
  content: String!
  author: User!
}

type User {
  id: ID!
  name: String!
  email: String!
  posts: [Post!]!
  createdAt: DateTime!
  updatedAt: DateTime!
}

So DateTime is not defined as a scalar

kamilkisiela commented 6 years ago

@Industrial I guess you can define DateTime scalar in the codegen and it should work, just like here https://github.com/dotansimha/graphql-code-generator/pull/444

Industrial commented 6 years ago

@kamilkisiela This is what I get now: https://gist.github.com/Industrial/a8135cc5f259aacf8c169efc37768d90

ardatan commented 6 years ago

@Industrial I think you don't need to define ID explicitly.

Industrial commented 6 years ago

If I leave out the ID scalar then I get the same error on ID

Industrial commented 6 years ago

@ardatan, @kamilkisiela: I'm not sure what to do with the ticket that @kamilkisiela pointed to.

He mentioned the "codegen". This is the domainmodel.graphl (or a URI) I'm using for the source? Or is there a configuration file that I should put data types like DateTime and ID in?

kamilkisiela commented 6 years ago

What you have is prisma config, not GraphQL Schema with queries, mutations and subscriptions, something that GraphQL server expects. GraphQL Codegen can't guess how you're data looks like so what you might eventually get is just a typescript interface for a model, no queries and other prisma generated things. If you would point gql-gen to server's endpoint or a file with introspection result it'd generate everything for you.

Urigo commented 6 years ago

@Industrial can you put up a small repo with an example? that way someone can make a nice PR and demonstrate how to do it

nemcek commented 5 years ago

Not sure if this is still an issue, but if I understood it correctly @Industrial you don't want to be generating types from your datamodel like that. If you want types for resolvers, you first need to generate the schema (the one you pass to the server) and then you can generate types for that schema. If you want types for your prisma (datamodel) you can do so using post hooks in prisma.yml and .graphqlconfig https://www.prisma.io/docs/prisma-cli-and-configuration/prisma-yml-5cy7/

Industrial commented 5 years ago

WHoa, @nemcek, I didn't know prisma had code generation itself :S. It seems they just released it. I'm closing this, then. Thanks for your time.