Different method of importing content types into schema.graphql

prisma

#1

It seems you can import content types into your schema file in 2 different ways, either by a # import:

# import Post from "./generated/prisma.graphql"

Or by explicitly typing it out:

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

Source:

What is the difference between the 2 methods? Im assuming it’s so you can prevent sensitive fields from being part of the API, is that right?


#2

Terminology

There are a couple of different terms and concepts that are important for this discussion.

Datamodel

The datamodel is the high level description/representation Note that this is only syntactic sugar to describe the datamodel from your database, this is not the final GraphQL schema exposed by the Prisma API.

You didn’t share your datamodel, so here’s an example. Consider this datamodel:

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

type Post {
  id: ID! @unique
  text: String!
  author: User!
}

The datamodel is represented by one or many .graphql files that are referenced in the datamodel property in the prisma.yml file.

Minor side comment, common words to talk about the entities User and Post from above are “type” or “model”. I haven’t heard “content type” a lot, but I think it’s a good name as well. Just mentioning that it’s not as common as the others.

GraphQL Schema for the Prisma API

Note that the datamodel from above is only syntactic sugar, using SDL to describe the datamodel on a high level. When a datamodel is deployed to a Prisma API, we obtain a very concrete representation of the resulting GraphQL API, called the GraphQL Schema. In your case this schema is stored in ./generated/prisma.graphql.

GraphQL Schema for your application

You are building a GraphQL Server, that also exposes a GraphQL API and an according GraphQL Schema. That schema is usually described in a schema.graphql file, and in the context of Prisma, schema.graphql has a direct relation to prisma.graphql from above: Some types from the Prisma API will end up in one way or another in your final GraphQL API.


With this terminology, we can start talking about your question about the difference between importing types and defining types in your application schema.

Importing types

# import Post from "./generated/prisma.graphql" will import the Post type plus will resolve all type dependencies transitively as well.

Importing types is transitive

You’ll see types like this in that schema file:

type Post implements Node {
  id: ID!
  text: String!
  author(where: UserWhereInput): User!
}

So, the import statement from above imports the following types:

  • Post, as seen in the above code snippet
  • UserWhereInput, because the author field of the Post type uses this as an input field
  • User, because the author field of Post uses this as return type
  • any other non-scalar type that is referred by the User type.

Defining types

On the other hand you can define types, by adding them to the final schema.graphql:

type Post {
  id: ID!
  text: String!
  author(where: UserWhereInput): User!
}

I wouldn’t call this importing, but rather defining the type. If you do this, you’ll likely end up with an error message because User and UserWhereInput are not defined. So either you have to define them, or import them.

Combining importing and defining types

We can combine importing and defining the same type.
Type definitions in schema.graphql have precedence over imported type definitions. You can use this dynamic to hide specific fields from a type, as you mentioned.