1.1.4 • Published 11 months ago

get-fields v1.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Get-Fields

get-fields was made with the intention of using the same graphql schema but with different fields. Where it is not necessary to create a new schema, but change the fields in the function call.

Install with npm

npm i get-fields

Usage

For example, if we want to create a schema to access user data, we can use this function in schema creation:

import { gql } from "@apollo/client";
import getFields from "get-fields";

export function GET_USER(...args) {
  const fields = getFields(args); // Returns the selected fields
  return gql`
    query {
      getUser {
        ${fields}  // Adds the selected fields inside the *route*
      }
    }`;
}

When using the schema, you must pass the fields you want to return separated by commas.

const Profile = () => {
  const { data } = useQuery(GET_USER("id", "name", "contact"));
  // [...]
};

But if you have to access nested data, you must use nesting object as in the example below:

const Table = () => {
  const { data } = useQuery(
    GET_SCHEDULE(
      "id",
      // object usage
      { name: "createdBy", items: ["id", "name", "contact"] },
      { name: "service", items: ["id", "duration", "price"] },
      "date",
      "status"
    )
  );
  // [...]
};

The nesting object are used to access the fields of the fields. All objects must have two properties:

  • name - Field name - String;
  • items - Array of fields that will be returned:
    • fields - String or nesting object.

Example of using the nesting object:

{ name: "createdBy", items: ["id", "name", "contact"]}

The example of using createdBy in graphql schema:

query {
  schedules {
    date
    createdBy {
      id
      name
      contact
    }
  }
}

Items can also receive nesting object

{ name: "date",  items: ["id", { name: "location", items: ["street", "house"] }] }

The above example in graphql schema:

query {
  schedules {
    date {
      id
      location {
        street
        house
      }
    }
  }
}
1.1.4

11 months ago

1.1.3

1 year ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago