Skip to content

Instantly share code, notes, and snippets.

View ziedHamdi's full-sized avatar
💭
Exploring opportunities

Zied Hamdi ziedHamdi

💭
Exploring opportunities
View GitHub Profile
@ziedHamdi
ziedHamdi / ssrPage.js
Last active August 27, 2021 13:16
Ssr with Apollo and Next.js: code in page
import {gql, useQuery} from '@apollo/client'
import {Grid} from "@material-ui/core";
import {serverSideTranslations} from 'next-i18next/serverSideTranslations';
import {addApolloState, initializeApollo} from '../apollo/client'
import {getDataFromTree} from "@apollo/client/react/ssr";
export async function getServerSideProps(context) {
const client = initializeApollo({})
// await getDataFromTree(SsrPage) (not working: see bug report)
const query1 = await client.query({query: ComplaintsQuery})
@ziedHamdi
ziedHamdi / _app.js
Created August 27, 2021 13:11
An example of _app.js
import React from 'react';
import {ApolloProvider} from '@apollo/client'
import {useApollo} from '../apollo/client'
import {ThemeProvider} from '@material-ui/core/styles';
import CssBaseline from '@material-ui/core/CssBaseline';
import Head from 'next/head';
import theme from '../components/theme';
import PropTypes from 'prop-types';
import {appWithTranslation} from 'next-i18next';
import {Provider} from 'next-auth/client'
@ziedHamdi
ziedHamdi / client.js
Last active March 15, 2022 09:48
Apollo client.js ssr enabled
import {useMemo} from 'react'
import {ApolloClient, gql} from '@apollo/client'
import merge from 'deepmerge'
import {cache} from './cache'
import {isSSR} from "../constants/util";
import isEqual from 'lodash/isEqual'
export const APOLLO_STATE_PROP_NAME = '__APOLLO_STATE__'
const typeDefs = gql`
@ziedHamdi
ziedHamdi / install.sh
Created September 15, 2021 10:16
build docker images from git repositories
#!/bin/sh
# GRAPHQL_SERVER_VERSION=$1
# NEXT_SERVER_VERSION=$2
# set variables from .env to system: https://gist.github.com/mihow/9c7f559807069a03e302605691f85572
if [ -f .env ]
then
export $(cat .env | sed 's/#.*//g' | xargs)
fi
@ziedHamdi
ziedHamdi / Dockerfile
Last active September 18, 2021 10:58
Example of docker file
# syntax=docker/dockerfile:1
# docker build . -t graphql:1.0.0
# docker run -d --name graphql_server graphql:1.0.0
# docker ps -a
# docker logs [name]
# docker exec -it [name] /bin/sh
# babel is under node_modules/.bin
# docker rm [name] //removes runtime conainer
# docker rm $(docker ps -a -q) //removes all stopped containers
# start: docker-compose up (-d for detached mode)
# stop: docker-compose down
version: "3.9"
services:
redis-server:
container_name: redis-server
image: 'redis:6.2-alpine'
networks:
- weally
# Instructions found in the following links
# https://docs.docker.com/engine/install/ubuntu/
# https://docs.docker.com/engine/install/linux-postinstall/
#: clean previous
sudo apt-get remove docker docker-engine docker.io containerd runc
#: install
sudo apt-get update
sudo apt-get install \
@ziedHamdi
ziedHamdi / graphql.js
Last active October 4, 2021 13:09
Next.js pages/api/ graphql endpoint
import logger from '../../lib/logger'
import {getSession} from "next-auth/client";
import dynamic from "next/dynamic";
function isTrueStr(str) {
return str === true ||
(str != null &&
str.toString().toLowerCase() === 'true')
}
@ziedHamdi
ziedHamdi / index.js
Last active October 4, 2021 11:25
implementation of getServerSideProps in pages/xyz.js files
export async function getServerSideProps(context) {
const session = await getSession(context)
const client = initializeApollo({})
//await getDataFromTree(Index) replaced by manual client.query calls
const cpl = await client.query({
query: ComplaintsQuery, context: {
headers: {'infra-token': session?.infraToken}
}
})
//...
@ziedHamdi
ziedHamdi / client.js
Created October 4, 2021 11:08
apollo client.js
import {useMemo} from 'react'
import {ApolloClient, HttpLink, gql} from '@apollo/client'
import merge from 'deepmerge'
import {cache} from './cache'
import {isSSR} from "../constants/util";
import isEqual from 'lodash/isEqual'
import logger from "../lib/logger";
export const APOLLO_STATE_PROP_NAME = '__APOLLO_STATE__'