2016-11-16 1 views
4

GraphQL에서 작업을 시작했습니다. 내 스키마에도 하나의 목록 항목이 포함되어 있습니다. 다음배열을 GraphQL에서 인수로 전달

내 스키마의 코드입니다 :

var userType = new graphql.GraphQLObjectType({ 
name: 'user', 
fields: function() { 
    return { 
    _id: { 
    type: graphql.GraphQLID 
    }, 
    name: { 
    type: graphql.GraphQLString 
    }, 
    age: { 
    type: graphql.GraphQLString 
    }, 
    degrees:[ 
    {type:graphql.GraphQLList} 
    ] 
} 
    } 
    }); 

다음과 같이 쿼리는 다음과 같습니다

var QueryType = new graphql.GraphQLObjectType({ 
    name: 'Query', 
    fields:() => ({ 
    userArr: { 
     type: new graphql.GraphQLList(userType), 
     args:{ 
     degrees:{type:new graphql.GraphQLList(userType)} 
     }, 
    resolve: function(source, args) { 
     console.log(args); 
     resolve(args); 
     } 
    } 
}) 
}) 

나는이 오류가 발생했습니다. enter image description here

기본적으로 나는 클라이언트의 graphql 쿼리에서 배열을 게시해야하고 그에 따라 쿼리를 정의해야한다. 이 문제에 대한 도움을 찾을 수 없기 때문에 제안이 있습니다 ..

답변

2

나는 최근에 아주 비슷한 것을했습니다. 입력 인수출력 데이터를 다시 보내도록 포맷 할 때와 동일한 유형의 시스템에 보유 할 필요가 없습니다. 그래서 당신의 arg는 단순히 문자열이나 객체의리스트, 또는 당신이 보내고 자하는 표준 타입을 받아 들일뿐입니다.

이 경우, 문자열 목록 (배열)을 받아들이도록 업데이트했습니다.

var QueryType = new graphql.GraphQLObjectType({ 
    name: 'Query', 
    fields:() => ({ 
    userArr: { 
     type: new graphql.GraphQLList(userType), 
     args:{ 
     degrees:{type:new graphql.GraphQLList(graphql.GraphQLString)} 
     }, 
    resolve: function(source, args) { 
     console.log(args); 
     resolve(args); 
     } 
    } 
}) 
}) 

사용자 유형에주의를 기울였습니다. 각도가 배열 괄호로 묶여 있습니다. 도 입력 인수와 마찬가지로 문자열 배열을 출력합니다. 이런 식으로 뭔가를 시도 :

degrees:{ 
    type:new graphql.GraphQLList(graphql.GraphQLString) 
    } 
코딩

해피!

+0

를 정의해야 위의 제안을 사용할 수 있습니다. ** user.degrees 필드 유형은 출력 유형이어야하지만 정의되지 않음 ** –

+0

userType의 코드가 올바른지 확인하십시오. –

+0

Gotcha, userType을 게시 할 수 있습니까? 원래 게시물을 편집하고 github gist code snippit을 게시하면 – Justin

0

GraphQLObjectType은 유효한 입력 유형이 아닙니다.

은 "입력 유형이 다른 개체, 기본적인 스칼라 유형 목록 유형 및 기타 입력 종류 필드를 가질 수 없습니다."

Mutations and Input Types를 참조하십시오 GraphQLString 그렇지 않으면 스칼라

degrees:{ 
    type:new graphql.GraphQLList(graphql.GraphQLString) 
} 

때문에

당신은 내가 지금은이 오류를주고도이 시도 가지고 GraphQLInputObjectType

const userInputType = new GraphQLInputObjectType({ 
    name: 'userInput', 
    fields: { /* put your fields here */ } 
}); 
/* some code in between */ 

degrees:{ 
    type:new graphql.GraphQLList(userInputType) 
}