Coder Social home page Coder Social logo

Algebraic interface types about graphql-spec HOT 9 OPEN

graphql avatar graphql commented on May 1, 2024
Algebraic interface types

from graphql-spec.

Comments (9)

sangeethasunkari avatar sangeethasunkari commented on May 1, 2024 4

My name is Khan

from graphql-spec.

sangeethasunkari avatar sangeethasunkari commented on May 1, 2024 4

KHHHHHHHHHAAAAANNNNN

from graphql-spec.

leebyron avatar leebyron commented on May 1, 2024 1

@Cyberlane, you can currently have an Object type implement as many interfaces as you like (the SmartPhone example) however there are currently no plans to implement intersection types (your findAll example) - for those cases you could use either another Interface which includes both sets of fields, or a Union type

from graphql-spec.

charypar avatar charypar commented on May 1, 2024

Following on the request for more real-world example: It is actually very similar to the one I used. It's a content fetching API and what I was trying to do were content collections. There are multiple types of collections (Page, Search, List) and then there are specific named collections at the top level (e.g. Editor's picks). Some of those named collections have a url associated to go to the collection specific page and that depends on a) the type of collection it is b) the specific collection (top stories for instance don't have a URL).

So what I'd like to do is have a Collection interface, which defines a collection has a title and items, and a Linkable interface that says there is an url field. Then I could say

queryType {
  top: Collection
  editors: Collection + Linkable
}

Without needing two specific interfaces for Collection and LinkableCollection (which is ok now, but I could imagine adding other "traits" later and things getting out of control). The other reason is I don't really want to leak the specific types up to the top level, because they may change based on where content gets fetched from, but the interface will likely stay the same.

Hope that helps to provide some more context around the idea.

from graphql-spec.

leebyron avatar leebyron commented on May 1, 2024

Thanks for the more concrete example, this is really interesting.

from graphql-spec.

alexyiu-elementum avatar alexyiu-elementum commented on May 1, 2024

Hi, @leebyron,

I have a couple of questions of GraphQL "interfaces".

(1)
I am wondering whether GraphQL allows a single Type implements Multiple Interfaces.
Using the example above, a related Type definition document would look like the following:

type ProfilePicture implements Linkable, Image {
    # skipping field declaration details here 
}

Based on Schema for Schema Introspection result in the RFC under "4.2 Schema Introspection",
https://facebook.github.io/graphql/#sec-Schema-Introspection
it looks like a single Type can implement multiple interfaces.

  # OBJECT only
  interfaces: [__Type!]

However, all the examples of Type definition document in RFC only show the case of implementing a single interface.

While I can find Grammar for Query Document, I cannot find Grammar for Type Definition Document.
"B.3 Query Document"
https://facebook.github.io/graphql/#sec-Appendix-Grammar-Summary.Query-Document
So, it did not help me to confirm whether this is a supported semantic or not.

(2)
Also, can an Interface "extend" from a bunch of other interfaces?
Based on the current comments under "4.2 Schema Introspection", it seems to imply "no".

Background Note :
Under JSON Schema, there is a construct of "allOf".
http://json-schema.org/latest/json-schema-validation.html#anchor82

We have used "allOf" in JSON Schema in a number of projects quite extensively.
From a technology migration viewpoint, it would be nice to allow:
(1) a single "type" "implements" multiple interfaces, and,
(2) a single "interface" "extends" multiple interfaces.

Alex Yiu
https://www.linkedin.com/in/alexyiu

from graphql-spec.

leebyron avatar leebyron commented on May 1, 2024

Yes, Objects may implement multiple interfaces. The experimental grammar for defining types works exactly the way you assumed, and you're correct in assuming so based on the fact that introspection returns a list of interfaces for an object type.

You can find the work-in-progress grammar for this at #90

Interfaces cannot extend other interfaces. There's no current plan to change this as it adds some avoidable complexity.

from graphql-spec.

alexyiu-elementum avatar alexyiu-elementum commented on May 1, 2024

@leebyron .... thank you for confirming this feature and quick turnaround time of answering my questions.

from graphql-spec.

Cyberlane avatar Cyberlane commented on May 1, 2024

@leebyron I don't suppose you have an expected ETA on when the multiple interface support is likely to be available?

Also, will this allow me to change my schema to something similar to this?

interface IHaveGPS {
  someSetting: String
  someOtherSetting: String
}

interface IHaveCellular {
  cellSetting: String
}

type GenericGPS implements IHaveGPS {
  someSetting: String
  someOtherSetting: String
}

type SmartPhone implements [IHaveGPS, IHaveCellular] {
  someSetting: String
  someOtherSetting: String
  cellSetting: String
}

type Query {
  findAll: [IHaveGPS, IHaveCellular]
}

from graphql-spec.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.