apollo-link-algolia
Advanced tools
Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "apollo-link-algolia", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Query Algoila with Apollo", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -12,3 +12,3 @@ ## Apollo-link-algolia | ||
* [Installation](#installation) | ||
* [Example](#example) | ||
* [Usage](#Usage) | ||
@@ -23,9 +23,50 @@ ## Installation | ||
## Example | ||
## Usage | ||
### Basic | ||
To get the results of your request, query for `hits` field | ||
```js | ||
const algoliaClient = algoliasearch('APPLICATION_ID', 'API_KEY') | ||
const algoliaLink = new AlgoliaLink({ client: algoliaClient }) | ||
const client = new ApolloClient({ | ||
link: algoliaLink, | ||
cache: new InMemoryCache() | ||
}) | ||
const LOCATIONS_QUERY = gql` | ||
query LocationsQuery { | ||
locationsInRadius @algolia(index: "dev_LOCATIONS", aroundLatLng: "40.71, -74.01", aroundRadius: 1000) | ||
locationsInRadius @algolia(index: "INDEX_NAME", aroundLatLng: "40.71, -74.01", aroundRadius: 1000) { | ||
hits | ||
} | ||
} | ||
` | ||
client.query({ query: LOCATIONS_QUERY }).then(response => console.log(response)) | ||
``` | ||
### Query meta fields | ||
Aside from the `hits` field, the result may contain several other fields that contain meta information: | ||
* `aroundLatLng` | ||
* `automaticRadius` | ||
* `disjunctiveFacets` | ||
* `exhaustiveFacetsCount` | ||
* `exhaustiveNbHits` | ||
* `facets` | ||
* `hierarchicalFacets` | ||
* `hitsPerPage` | ||
* `index` | ||
* `nbHits` | ||
* `nbPages` | ||
* `page` | ||
* `parsedQuery` | ||
* `processingTimeMS` | ||
* `query` | ||
* `queryID` | ||
* `serverUsed` | ||
* `timeoutCounts` | ||
* `timeoutHits` | ||
* `userData` | ||
* `_rawResults` | ||
* `_state` |
import algoliasearchHelper from 'algoliasearch-helper' | ||
import { ApolloLink, Observable } from 'apollo-link' | ||
import { addTypenameToDocument, getMainDefinition, hasDirectives } from 'apollo-utilities' | ||
import { ApolloLink, Observable } from 'apollo-link' | ||
import { graphql } from 'graphql-anywhere/lib/async' | ||
@@ -43,5 +43,13 @@ | ||
const addTypeNameToResult = (result, __typename = 'AlgoliaQuery') => ({ __typename, ...result.content }) | ||
const resolver = (fieldName, root, args, context, info) => { | ||
const { directives } = info | ||
const { directives, isLeaf, resultKey } = info | ||
const isNotAlgoliaQuery = !directives || !directives.algolia | ||
if (isLeaf || isNotAlgoliaQuery) { | ||
const returnValue = (root || {})[resultKey] || (root || {})[fieldName] | ||
return returnValue !== undefined ? returnValue : null | ||
} | ||
if (!directives.algolia.index) { | ||
@@ -59,3 +67,3 @@ throw new Error('Algolia index name is required') | ||
return helper.searchOnce() | ||
return helper.searchOnce().then(result => addTypeNameToResult(result)) | ||
} | ||
@@ -62,0 +70,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12339
92
71