2017-08-25 5 views
0

にインターフェイスを持つ労働組合のタイプを使用するには?どちらか一方が有効である -どのように私は労働組合の種類とインターフェースを考えてみましょうgraphql

type WidgetA implements WidgetInterface { 
    id: ID! 
    name: String! 
    description: String 
    type: ??? 
} 
+0

あなたの質問から、あなたが求めていることは不明です。この文脈で「ウィジェット」とは何ですか? A、B、C以外のすべての型定義はすでにスキーマに入っていますので、正確に何を定義しようとしていますか?または、このスキーマのリゾルバを実装して動作させる方法について質問していますか? –

+0

私の質問は、インターフェイスを実装する型の作成についてです。クエリを更新しました。 –

答えて

1

タイプは労働組合(type: WidgetType)または特定のタイプ(type: A)することができます。

は、ここでそれはあなたがタイプのために労働組合を使用している場合のように見えるかもしれない方法の簡単な例です:

import { makeExecutableSchema } from 'graphql-tools'; 

const typeDefs = ` 
    type Query { 
    hello: String 
    widgets: [WidgetInterface] 
    } 
    union WidgetType = A | B | C 
    interface WidgetInterface { 
    id: ID! 
    name: String! 
    type: WidgetType! 
    } 
    type A { 
    foo: String 
    } 
    type B { 
    bar: String 
    } 
    type C { 
    baz: String 
    } 
    type WidgetA implements WidgetInterface { 
    id: ID! 
    name: String! 
    description: String 
    type: WidgetType! 
    } 
`; 

const widgets = [ 
    { 
    id: 1, 
    name: 'Foo', 
    description: '', 
    type: { 
     baz: 'Baz' 
    } 
    } 
] 

const resolvers = { 
    Query: { 
    hello: (root, args, context) => { 
     return 'Hello world!'; 
    }, 
    widgets:() => { 
     return widgets; 
    }, 
    }, 
    WidgetInterface: { 
    __resolveType:() => 'WidgetA' 
    }, 
    WidgetType: { 
    __resolveType: (obj) => { 
     if (obj.foo) return 'A' 
     if (obj.bar) return 'B' 
     if (obj.baz) return 'C' 
    } 
    } 
}; 

export const schema = makeExecutableSchema({ 
    typeDefs, 
    resolvers, 
}); 

あなたはcopy and paste this into Launchpadアクションでそれを見ることができます。

+0

しかし、これは、「WidgetA」はあらゆるタイプのものである可能性があります。私は、ウィジェットの種類を修正する方法があると思った。 –

+0

これを修正する必要がある場合は、単に 'type:A'やそれが必要なタイプをしないでください。組合の一部であるという理由だけで、それを別々に使うことはできません。 –

+0

説明をありがとう。 –

関連する問題