2015-08-31 16 views
19

'da (Array) alanlarının bir listesini eklemek için bir mutasyon sorgusu nasıl yapılır yakın zamanda GraphQL üzerinde çalışmaya başladım, veriyi düz şemaya herhangi bir sorun olmadan ekleyebildim ama bir Veri dizisi söz konusu olduğundaGraphQL

EventType = new GraphQLObjectType({ 
    name: 'Event', 
    description: 'A Event', 
    fields:() => ({ 
    _id: { 
     type: GraphQLString, 
     description: 'The id of the event.', 
    }, 
    id: { 
     type: GraphQLString, 
     description: 'The id of the event.', 
    }, 
    title: { 
     type: GraphQLString, 
     description: 'The title of the event.', 
    }, 
    description: { 
     type: GraphQLString, 
     description: 'The description of the event.', 
    }, 
    media:{ 
     type:new GraphQLList(mediaType), 
     description:'List of media', 
    }, 
    location:{ 
     type:new GraphQLList(locationType), 
     description:'List of location', 
    } 
    }) 
}); 

// Media Type 

export var mediaType = new GraphQLObjectType({ 
    name: 'Media', 
    description: 'A Media', 
    fields:() => ({ 
    _id: { 
     type: GraphQLString, 
     description: 'The id of the event.', 
    }, 
    url:{ 
     type: GraphQLString, 
     description: 'The url of the event.', 
    }, 
    mediaType:{ 
     type: GraphQLString, 
     description: 'The mediaTypa of the event.', 
    } 
    }) 
}); 

// Location Type 

export var locationType = new GraphQLObjectType({ 
    name: 'Location', 
    description: 'A location', 
    fields:() => ({ 
    _id: { 
     type: GraphQLString, 
     description: 'The id of the event.', 
    }, 
    address:{ 
     type: GraphQLString, 
     description: 'The address.', 
    }, 
    state:{ 
     type: GraphQLString, 
     description: 'The state.', 
    }, 
    city:{ 
     type: GraphQLString, 
     description: 'The city.', 
    }, 
    zip:{ 
     type: GraphQLString, 
     description: 'The zip code.', 
    }, 
    country:{ 
     type: GraphQLString, 
     description: 'The country.', 
    } 
    }) 
}); 
: benim mutasyon sorgusu,

{ "errors": [ { "message": "Must be input type" } ]} 

Ben postacı kullanarak sorgu test ediyorum gibi bir hata

mutation M { 

AddEvent 
    (

    title: "Birthday event" 

    description:"Welcome to all" 

    media:[{url:"www.google.com", mediaType:"image" }] 

    location:[{address:{state:"***", city:"****"}}] 

    ) 

{title,description,media,location,created,_id}} 

Bu benim Olay Şeması edilir alma

Gelincik Şeması:

var EventSchema = new mongoose.Schema({ 
    title: { 
     required: true, 
     type: String, 
     trim: true, 
     match: /^([\w ,.!?]{1,100})$/ 
    }, 
    description: { 
     required: false, 
     type: String, 
     trim: true, 
     match: /^([\w ,.!?]{1,100})$/ 
    }, 
    media: [{ 
     url: { 
      type: String, 
      trim: true 
     }, 
     mediaType: { 
      type: String, 
      trim: true 
     } 
    }], 
    location: [{ 
      address: { 
       type: String 
      }, 
      city: { 
       type: String 
      }, 
      state: { 
       type: String 
      }, 
      zip: { 
       type: String 
      }, 
      country: { 
       type: String 
      } 
    }] 
}) 

Mutasyon Tipi:

addEvent: { 
     type: EventType, 
     args: { 

     _id: { 
      type: GraphQLString, 
      description: 'The id of the event.', 
     }, 
     title: { 
      type: GraphQLString, 
      description: 'The title of the event.', 
     }, 
     description: { 
      type: GraphQLString, 
      description: 'The description of the event.', 
     }, 
     media:{ 
      type:new GraphQLList(mediaType), 
      description:'List of media', 
     }, 
     location:{ 
      type:new GraphQLList(locationType), 
      description:'List of media', 
     }, 
     created: { 
      type: GraphQLInt, 
      description: 'The created of the user.',  
     } 
     }, 
     resolve: (obj, {title,description,media,location,created,_id}) => { 

     let toCreateEvent = { 
      title, 
      description, 
      created:new Date(), 
      start: new Date(), 
      media, 
      location, 
      _id, 
     }; 

     return mongo() 
      .then(db => { 
       return new Promise(
       function(resolve,reject){ 
       let collection = db.collection('events'); 
        collection.insert(toCreateEvent, (err, result) => { 
        db.close(); 

        if (err) { 
         reject(err); 
         return; 
        } 
        resolve(result); 
        }); 
      }) 
      }); 
     } 
    } 
+0

Buna benzer bir şey yaptım (dizileri kullanarak) ve çalışır. Şemanınızı paylaşabilir misiniz? – mfirry

+0

Merhaba mfirry, Mongoose ve GraphQL Şemalarımı gönderiye ekledim. Lütfen kontrol edin ve mümkün olan en kısa sürede bana cevap verin. Teşekkür ederim!! – Mahesh

+0

Ayrıca, 'AddEvent' – mfirry

cevap

19

Kişisel Sorun, mutasyonların tanımlarken, her türlü giriş tipleri, sen "Must be input type" olsun dolayısıyla hata olması gerektiğidir. Burada (sizin mutasyondan) Yani:

media:{ 
    type:new GraphQLList(mediaType), 
    description:'List of media', 
}, 
location:{ 
    type:new GraphQLList(locationType), 
    description:'List of media', 
}, 

GraphQLList, mediaType ve locationType girdi tipleri olmalıdır. Bir giriş türü (giriş türleri olarak düşünülen GraphQL tiplerinin listesini görmek için buraya bakınız) (bkz. Burada https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L74-L82), zaten bir giriş türüdür (bkz. Buradaki).

Ancak sizin tipleri mediaType ve locationType bir giriş türü değil GraphQLObjectType türünden olmakta ancak tekrar giriş türleri listesinde bakarsak: https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L74-L82, sen bir nesne girdi türüdür GraphQLInputObjectType bulmak, bu yüzden ne yapacaksınız onların "giriş" sürümü tarafından mediaType ve locationType yerine yapmanız gereken.

Ne yapmak önermek mediaType ve locationType aynı alan yapıya sahip fakat new GraphQLInputObjectType({... yerine new GraphQLObjectType({... ile oluşturulan ve mutasyon bunları kullanmak istiyorsunuz mediaInputType ve locationInputType yaratmaktır.

Aynı soruna rastladım ve bunu çözdüm, herhangi bir sorunuz varsa yorum yapmaktan çekinmeyin.

+0

Bu harika bir şey ama benim için işe yaramıyor .. Ne yapıyorum yanlış? Heres benim mutationType https://gist.github.com/piq9117/64cee784677915edc1da8ffec7ffa383 – ken

1

Aynı soruna rastladım - Giriş tanımı içinde nesne dizisini nasıl belirleyeceğimi bilmiyordum. isteyenler bir "metin" şema çözüm görmek için çok:

type Book { 
    title: String! 
} 

girişinizi tipinde Kitaplar bir dizi olması

input AuthorInput { 
    name: String! 
    age: Int! 
} 

sadece giriş deyimi içimde books: [Book!] ekleyemezsiniz (isterseniz çoğaltmak) gerekli alanları içeren giriş tipini yaratmak kasten gerekir:

input BookInput { 
    title: String! 
} 

ve sonra şunları yapabilirsiniz:

input AuthorInput { 
    name: String! 
    age: Int! 
    books: [BookInput!] 
} 
İlgili konular