Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example using two data sets related #112

Open
somecallmemike opened this issue Aug 18, 2017 · 1 comment
Open

Example using two data sets related #112

somecallmemike opened this issue Aug 18, 2017 · 1 comment

Comments

@somecallmemike
Copy link

I am brand new to graphql and I am having trouble deciphering how to link two related data sets together using this tool. I created a company-database.ts:

export const companies = [
  {
    id: "1",
    name: 'APM'
  },
  {
    id: "2",
    name: 'DSP'
  },
  {
    id: "3",
    name: 'APP'
  }
];

export const findCompany = (companies: Array<any>, id: string) => {
  return companies.find(company => company.id === id);
};

export const addCompany = (companies: Array<any>, company: any) => {
  company.push(company);
  return company;
};

As well as a company-type.ts that adds PersonType as a related node. I also added a companyId field to persons:

export const typeDef = `
type CompanyType {
    name: String
    id: String
    persons: [PersonType]
}
`;

export const resolver = {
  CompanyType: {
    matches(root, args, ctx) {
      return ctx.companies;
    }
  },
};
export const persons = [
  {
    id: "1",
    sex: 'male',
    name: 'miro',
    companyId: '1'
  },
  {
    id: "2",
    sex: 'female',
    name: 'lala',
    companyId: '2'    
  },
  {
    id: "3",
    sex: 'male',
    name: 'joe',
    companyId: '3'    
  }
];

And added everything to query.ts:

export const typeDef = `
# Root Query
type Query {
    testString: String
    testStringConnector: String
    rootMockedString: String
    mockedObject: MockedType
    someType: SomeType
    getPerson(id: String!): PersonType
    persons: [PersonType]
    getCompany(id: String!): CompanyType
    companies: [CompanyType]
}
`;

export const resolver = {
  Query: {
    getPerson(root, args, ctx) {
      return ctx.findPerson(ctx.persons, args.id);
    },
    persons(root, args, ctx) {
      return ctx.persons;
    },
    getCompany(root, args, ctx) {
      return ctx.findCompany(ctx.comanies, args.id);
    },
    companies(root, args, ctx) {
      return ctx.comanies;
    },
    testString() {
      return "it Works!";
    },
    testStringConnector(root, args, ctx) {
      return ctx.testConnector.testString;
    },
    someType(root, args, ctx) {
      return {testFloat: 303.0303, testInt: 666};
    },
  },
};

However the system is erroring with

CompanyType.matches defined in resolvers, but not in schema

Is there a special method for forming this relationship that I am not seeing?

@DxCx
Copy link
Owner

DxCx commented Aug 19, 2017

hey!

did you require the new company file?
(https://github.com/DxCx/webpack-graphql-server/blob/master/src/schema/index.ts#L6)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants