Code Monkey home page Code Monkey logo

Comments (4)

EmergentBehavior avatar EmergentBehavior commented on July 2, 2024

Is definitely in graphql-js, see this test:

https://github.com/graphql/graphql-js/blob/a941554fc56d607fecebe2188a7ca8848835c2a8/src/execution/__tests__/abstract.js#L44-L52

from graphql.

bsr203 avatar bsr203 commented on July 2, 2024

oops.. ya, I am not sure how I missed it in the search. We don't use it now, so may keep this open to track it ???

from graphql.

sogko avatar sogko commented on July 2, 2024

Thanks @EmergentBehavior for pulling that out from graphql-js.

For reference, here's the ported test:

func TestIsTypeOfUsedToResolveRuntimeTypeForInterface(t *testing.T) {

To elaborate further on IsTypeOf, it's usually would be used to check for the type of the value at runtime.
More specifically, for abstract types (i.e. interfaces and unions).

So a typical use-case would be when you have the following schema (as defined in the TestIsTypeOfUsedToResolveRuntimeTypeForInterface test)

// Using GraphQL type system shorthand notation, refer to: http://graphql.org/docs/typesystem/

// `Pet` is an interface, with a `name` field
interface Pet {
  name: String
}
// `Dog` implements `Pet`
type Dog : Pet {
  name: String
  woofs: Boolean
}
// `Cat` implements `Pet`
type Cat : Pet {
  name: String
  meows: Boolean
}
type Query {
  pets: [Pet] // `pets` returns a list of `Pet` interface
}

So, given the following query:

query Q {
  pets {
    name
  }
}

The Resolve function for pets field may return list of values that are of a Cat type or a Dog type (or any other type that implements Pet interface).

IsTypeOf function then will be executed at runtime to determine it's concrete type, i.e. if the value return is a Cat type or a Dog type.

Regarding it being on the spec, it is there but it's very subtle:

The interface type should have some way of determining which object a given result corresponds to. Once it has done so, the result coercion of the interface is the same as the result coercion of the object.

The union type should have some way of determining which object a given result corresponds to. Once it has done so, the result coercion of the union is the same as the result coercion of the object.

IsTypeOf is just one way of implementing that spec.

Note that IsTypeOf is defined in the concrete types (i.e. in graphql.ObjectConfig)
You can also choose to define ResolveType function in the abstract types (see graphql.InterfaceConfig and graphql.UnionConfig).

So graphql-js/graphql-go provides two ways to satisfy the above specs.

Hope that make sense, cos it's past midnight here lol. Cheers!

from graphql.

bsr203 avatar bsr203 commented on July 2, 2024

@sogko thanks you so much for your time explaining it. I did a code search and missed the the usage (it was few commits behind). Thanks again.

from graphql.

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.