graphql-compose-mongoose icon indicating copy to clipboard operation
graphql-compose-mongoose copied to clipboard

how to get specific value

Open atteia opened this issue 7 years ago • 5 comments

in this example

how to get value of skill for English language skills only not list all languages skills ??

atteia avatar Apr 04 '18 09:04 atteia

is there any easy solution ? I will make a separate title schema for skills with Add relation for them and use search , I don't know if it will work as I wont or not ! also the problem I have many cases like that, it'll be hard to add a separate file for every case .

atteia avatar Apr 08 '18 12:04 atteia

You may add a new field with your filtering logic. Eg.

UserTC.setField('languageSkill', {
  type: 'String', // output type
  args: { language: 'String!' }, // your argument which will accept language
  resolve: (source, args) => {
    if (!source.languages) return null;
    const t = source.languages.filter(o => o && o.language === args.language);
    return t[0] ? t[0].skill : null;
  },
  projection: { languages: 1 }, // need for requesting `languages` from server
});

If you have many places where you need to add the same custom logic, just write your helper function, which accept TypeComposer makes some modifications and return them back:

function extendBySuperLogic(tc: TypeComposer): TypeComposer {
  tc.setField(...);
  tc.addFields(...);
  tc.extendField(...);
  tc.some_other_methods
  return tc;
}

nodkz avatar Apr 20 '18 08:04 nodkz

I got an error


export const UserTC = composeWithRelay(composeWithMongoose(User));
UserTC.addField('languageSkill', {
  type: 'String', // output type
  args: { language: 'String!' }, // your argument which will accept language
  resolve: (source, args) => {
    if (!source.languages) return null;
    const t = source.languages.filter(o => o && o.language === args.language);
    return t[0] ? t[0].skill : null;
  },
  projection: { languages: 1 }, // need for requesting `languages` from server
});

TypeError: UserTC.addField is not a function

atteia avatar Apr 26 '18 18:04 atteia

I think you can use UserTC.addFields()

jcortes avatar Apr 26 '18 21:04 jcortes

Oh, my miss. May be used such methods:

setField(name, config);
Or
addFields({
  name1: config1
  name2: config2
});

nodkz avatar Apr 27 '18 02:04 nodkz