Why does GraphQL "implement" the need for duplicate fields, is this mandatory? If so, what are the main reasons?

Why implementsshould the GraphQL keyword duplicate fields, is this mandatory? As examples in the document:

enum Episode { NEWHOPE, EMPIRE, JEDI }

interface Character {
  id: String
  name: String
  friends: [Character]
  appearsIn: [Episode]
}

type Human implements Character {
  id: String
  name: String
  friends: [Character]
  appearsIn: [Episode]
  homePlanet: String
}

type Droid implements Character {
  id: String
  name: String
  friends: [Character]
  appearsIn: [Episode]
  primaryFunction: String
}

If so, what are the main reasons?

Coz If I need to duplicate, if I change, then I need to change everywhere ...

+4
source share
2 answers

Yes, this is mandatory in accordance with the current specification :

The type of object must contain a field with the same name for each field defined in the interface.

. . , , . .

interface Character {
  id: String
  name: String
  friends: [Character]
  appearsIn: [Episode]
}

type Human implements Character {
  id: String
  name: String
  friends: [Human] # <- notice Human here
  appearsIn: [Episode]
  homePlanet: String
}

type Droid implements Character {
  id: String
  name: String
  friends: [Droid!]! # <- specified Droid here + added not null
  appearsIn: [Episode]
  primaryFunction: String
}

, ( ).

:

+7

, . , Java- . , . , , . , .

, GraphQL JavaScript graphql-js. - . "" resolve, resolveType , .

, , , "". , . , .

const characterFields = `
  id: String
  name: String
  friends: [Character]
  appearsIn: [Episode]
`

const typeDefs = `
  interface Character {
    ${characterFields}
  }

  type Human Implements Character {
    ${characterFields}
    homePlanet: String
  }
`

EDIT: Java, . RomanHotsiy, .

+2

Source: https://habr.com/ru/post/1674961/


All Articles