Graphcool / graphcool-framework

Apache License 2.0
1.77k stars 130 forks source link

Unclear error message when changing a relation field to a scalar field #423

Open marktani opened 6 years ago

marktani commented 6 years ago

Issue by aloof-ruf Monday Nov 27, 2017 at 17:19 GMT Originally opened as https://github.com/graphcool/prisma/issues/1334


Current behaviour Right now, when you have previously locally deployed your types with a relation, and then try to locally deploy an update that removes the previous relation but keeps the name of it and now references a scalar type you'll get the following misleading error: You can only create scalar fields and <scalar type here> is not a scalar value. Did you intend to create a relation?

Reproduction You can reproduce this with the following steps:

  1. Create a new project (not explicitly required, but easier this way) by making a fresh directory and initialising Graphcool: mkdir graphcool_local_fail; cd graphcool_local_fail; graphcool init
  2. Go into types.graphql and uncomment the Post type along with the posts relation in the User type. The file should look like this:
    
    # The following types define the data model of the example service
    # based on which the GraphQL API is generated

type User @model { id: ID! @isUnique name: String dateOfBirth: DateTime

Uncomment below - you can declare relations between models like this

posts: [Post!]! @relation(name: "UserPosts") }

Uncomment the model below as well

type Post @model { id: ID! @isUnique title: String!

Every relation also required a back-relation (to determine 1:1, 1:n or n:m)

author: User! @relation(name: "UserPosts") }

  3. Spin up local docker containers (`graphcool local up`) and deploy locally (`graphcool deploy`)
  4. Now change the `types.graphql` again, this time removing the relation but  **keeping the field name (`posts` in this case)**. The file should now look like this:

The following types define the data model of the example service

based on which the GraphQL API is generated

type User @model { id: ID! @isUnique name: String dateOfBirth: DateTime

posts: Int }

Uncomment the model below as well

type Post @model { id: ID! @isUnique title: String! }

  5. Your migration includes potentially destructive changes. When you try to deploy with `graphcool deploy --force` you will get the following error:
```sh
 % graphcool deploy --force
Bundling functions... 820ms
Deploying to local with target dev locally... 150ms
There are issues with the new service definition:

  Global
    ✖ You can only create scalar fields and 'Int' is not a scalar value. Did you intend to create a relation?

Expected behavior? The current way around this is to completely remove the field (in the case above, remove posts) and then insert it again as whatever you want. I feel Graphcool should either handle this change or give a better error message such as "posts" field already references a relation. Please choose another name for your scalar field. Think about potentially ambiguous field names like data, where at one time it references a particular model, but over time the developers decided to just reference a String. I feel like a more explicit error would help those developers.

marktani commented 6 years ago

Comment by marktani Friday Dec 01, 2017 at 14:41 GMT


Thanks a lot for the detailed write up, @aloof-ruf! We're currently discussing a new migrations design, would love to hear your insights there: https://github.com/graphcool/framework/issues/1263 🙂