2017-05-26 110 views
1

這是我的graphql模式,查詢和變異。graphql必填字段方法

我用「!」在我的模式中標記了必填字段

如何創建突變來添加新客戶端?

我真的需要再次寫相同的必填字段嗎?

createClient(contactMethod: String!, hearAbout: String! .........): Client


const typeShard = ` 
    type ClientProfile { 
    name: String! 
    surname: String! 
    address: String 
    language: String! 
    } 

    type Client { 
    _id: String 
    isEdit: Boolean 
    createdAt: String 
    shortId: Int 
    profile: ClientProfile 
    comments: String 
    contactMethod: String! 
    hearAbout: String! 
    leadAgentId: String 
    branchId: String! 
    } 
`; 

const queryShard = ` 
    getAllClients: [Client] 
`; 

const mutationShard = ` 
    removeClient(shortId : Int!): Client 
    createClient(contactMethod: String!, hearAbout: String! ......... ): Client 
`; 

const resolvers = { 
    Query: { 
    getAllClients:() => MongoClients.find().fetch(), 
    }, 
    Mutation: { 
    removeClient(root, { shortId }) { 
     const client = MongoClients.findOne({ shortId }); 
     if (!client) throw new Error(`Couldn't find client with id ${shortId}`); 
     MongoClients.remove({ shortId }); 
     return client; 
    }, 
    createClient: (_, args) => { 
     return MongoClients.insert(args); 
    }, 
    }, 
}; 

回答

1

你不需要寫每一個突變相同的字段。您可以定義一個input類型。請看看這個cheat sheet

所以你的情況可能是這樣的:

const typeShard = ` 
 
    type ClientProfile { 
 
    name: String! 
 
    surname: String! 
 
    address: String 
 
    language: String! 
 
    } 
 

 
    type Client { 
 
    _id: String 
 
    isEdit: Boolean 
 
    createdAt: String 
 
    shortId: Int 
 
    profile: ClientProfile 
 
    comments: String 
 
    contactMethod: String! 
 
    hearAbout: String! 
 
    leadAgentId: String 
 
    branchId: String! 
 
    } 
 
    input ClientInput { 
 
    contactMethod: String! 
 
    hearAbout: String! 
 
    ..... 
 
    } 
 
`; 
 

 
const mutationShard = ` 
 
    removeClient(shortId : Int!): Client 
 
    createClient(clientInput: ClientInput!): Client 
 
`;

+0

謝謝您的回答。我讀過它。 但是可以將輸入和輸出結合起來嗎?在很多情況下,這將是代碼重複。任何想法? – Gemmi

+1

我不認爲這是可能的 –