2017-12-27 9 views
0
const { 
    makeExecutableSchema 
} = require('graphql-tools'); 
const resolvers = require('./resolvers'); 

const typeDefs = ` 

    type Link { 
    args: [Custom] 
    } 

    union Custom = One | Two 

    type One { 
    first: String 
    second: String 
    } 

    type Two { 
    first: String 
    second: [String] 

    } 

    type Query { 
    allLinks: [Link]! 

    } 

`; 

const ResolverMap = { 
    Query: { 
    __resolveType(Object, info) { 
     console.log(Object); 
     if (Object.ofType === 'One') { 
     return 'One' 
     } 

     if (Object.ofType === 'Two') { 
     return 'Two' 
     } 
     return null; 
    } 
    }, 
}; 

// Generate the schema object from your types definition. 
module.exports = makeExecutableSchema({ 
    typeDefs, 
    resolvers, 
    ResolverMap 
}); 


//~~~~~resolver.js 
const links = [ 
    { 
     "args": [ 
      { 
       "first": "description", 
       "second": "<p>Some description here</p>" 
      }, 
      { 
       "first": "category_id", 
       "second": [ 
        "2", 
        "3", 
       ] 
      } 

     ] 
    } 
]; 
module.exports = { 
    Query: { 
     //set data to Query 
     allLinks:() => links, 
     }, 
}; 

graphqlのドキュメンタリーが非常に悪いので混乱します。私は、どのようにプロパティや関数をスキーマ内で使用できるようにするためにresolveMap関数を適切に設定するか分かりません。現時点では、クエリの実行を使用しているときに、生成されたスキーマでInterfaceまたはUnionの型を実行できないというエラーが表示されます。このスキーマを適切に実行するにはどうすればよいですか?GraphqlJS-タイプの競合 - 共用体またはインタフェースを使用できません

答えて

1

resolversResolverMapは、ともにresolversと定義する必要があります。また、タイプリゾルバはCustomユニオンタイプに対して定義し、Queryでは定義しないでください。

const resolvers = { 
    Query: { 
    //set data to Query 
    allLinks:() => links, 
    }, 
    Custom: { 
    __resolveType(Object, info) { 
     console.log(Object); 
     if (Object.ofType === 'One') { 
     return 'One' 
     } 

     if (Object.ofType === 'Two') { 
     return 'Two' 
     } 
     return null; 
    } 
    }, 
}; 

// Generate the schema object from your types definition. 
const schema = makeExecutableSchema({ 
    typeDefs, 
    resolvers 
}); 

更新: OPはエラー"Abstract type Custom must resolve to an Object type at runtime for field Link.args with value \"[object Object]\", received \"null\"."を得ていました。 ofTypeというフィールドがObjectの内部にないため、タイプリゾルバObject.ofType === 'One'Object.ofType === 'Two'の条件が常にfalseであるためです。したがって解決された型は常にnullです。 `「抽象型カスタムに解決する必要があります。これを修正するには

、どちらかargs配列(resolvers.jslinks定数)の各項目にofTypeフィールドを追加したり、typeof Object.second === 'string'のようなものに条件を変更してArray.isArray(Object.second)

+0

イムは今、エラーを取得します実行時に、Link.argsフィールドの値が\ "[Object Object] \"で、\ "null \"を受け取ったオブジェクトタイプ。 "'カスタムタイプを定義する必要がありますか?何を含めるべきか? – Brunhilda

+1

'Object.ofType === 'One''と' Object.ofType ===' Two''は常に 'false'ですからです。だから解決された型は常に 'null'です。 'args'配列の各項目に' ofType'フィールドを追加してみてください( 'resolvers.js'の' links'定数)。条件を 'typeof Object.second === 'string''と' Array.isArray(Object.second) 'に変更してください。私は今私のコンピュータではないので、私は確認できません。 – Bless

+0

オハイオ州神よ、あなた、私はあなたを愛しています。私は数週間それについて考えている、人々の束が私を助けようとしたし、その長い時間の後、最終的にアスワーがあります。あなたは私の救い主です。あなたは今、どれくらい感謝しているのか分かりません。 – Brunhilda

関連する問題