Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local Abstract Type Settings #623

Open
jasonkuhrt opened this issue Nov 13, 2020 · 0 comments
Open

Local Abstract Type Settings #623

jasonkuhrt opened this issue Nov 13, 2020 · 0 comments
Labels
scope/abstract-types type/feat Add a new capability or enhance an existing one

Comments

@jasonkuhrt
Copy link
Member

jasonkuhrt commented Nov 13, 2020

As mentioned in the docs...

Currently Nexus only supports global abstract type strategy settings. In the future however (#623) there may be ways to enable a particular strategy for only a subset of your schema. This way you could for example express that __typename is required in the returned model data of resolvers for one abstract type but require all other objects whom are members of other abstract types to implement isTypeOf.

Ideally something like:

queryType({
  definition(t) {
    t.field('somethingElse', {
      type: 'SomethingElse',
      // ...
    })

    t.field('search', {
      type: 'SearchResult',
      args: {
        pattern: stringArg(),
      },
      resolve(_, args, ctx) {
        return ctx.db.search(args.pattern).map(result => {
          return {
            ...result,
            __typename: 'Photo', // <-- This property is __REQUIRED__
          }
        })
      },
    })
  },
})

unionType({
  name: 'SearchResult',
  strategy: 'DiscriminantModelField', // <-- Set the strategy only for this abstract type
  definition(t) {
    t.members('Photo' /* ... */)
  },
})

objectType({
  name: 'Photo',
  definition(t) {
    t.string('url')
    t.int('width')
    t.int('height')
  },
})

unionType({
  name: 'SomethingElse',
  strategy: 'Modular', // <-- Set the strategy only for this abstract type
  definition(t) {
    t.members('Thing1', /* ... */)
  },
})

objectType({
  name: 'Thing1',
  isTypeOf(data) { // <-- REQUIRED b/c object is member of "SomethingElse" which uses Modular strategy
    // ...
  },
})
@jasonkuhrt jasonkuhrt added type/feat Add a new capability or enhance an existing one scope/abstract-types labels Nov 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope/abstract-types type/feat Add a new capability or enhance an existing one
Projects
None yet
Development

No branches or pull requests

1 participant